Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

how to convert an 'unsigned short *' variable to 'char *'

Posted on 2003-02-27
9
Medium Priority
?
250 Views
Last Modified: 2010-05-18
hi all,

i need to compare an 'unsigned short *' variable which has a value = 8030 with a 'char *' variable which has a value  "8030".
Please tell me how to do it. I am not able to circumvent the problem as  typecasting wont work.... you probably know why!!
thanks in advance

regards,
srikant
0
Comment
Question by:srik18
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 6

Accepted Solution

by:
zebada earned 90 total points
ID: 8036548
#include <stdlib.h>
#include <stdio.h>
int
main()
{
  unsigned short v=8030;
  unsigned short *u=&v;
  char *s="8030";

  if ( (unsigned short)atoi(s)==*u )
    printf("Equal\n");
  else
    printf("Not equal\n");
}
0
 
LVL 6

Expert Comment

by:gj62
ID: 8036584
Hmmm,

char * pStr = "8030"; is a string - it contains 5 bytes of memory, the first character being '8', next is '0', etc.

unsigned short * pShort = 8030; is a short integer - on 32bit systems, taking 2 bytes, and represented by hex 0x1f53.  

Clearly, two different values...

In order to compare, you could :


unsigned short sInt = 8030;
unsigned short *pShort;
pShort = &sInt;
char * pChar = "8030";

if ( *pShort == atoi(pChar) )
{
     printf("THE EXPRESSIONS ARE THE EQUAL");
}
0
 

Author Comment

by:srik18
ID: 8036777
hey ppl,
thanks for ur replies.
First of all i know the problem with the question i asked,i tried converting both to int but that wont work either as that will conversion from unsigned short* to int will make u loose data, i know the theory behind my problem, help me out with the solution , if there is one. My problem is that this is realllllllllly big program and the input is an unsigned short* NOT unsigned short, and i cant convert it either. and i need a way to convert unsigned short* to char *, not from unsigned short to char*.
please kindly let me know if u know how to do it.
thnx,
srikant.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:srik18
ID: 8036795
hey ppl,
thanks for ur replies.
First of all i know the problem with the question i asked,i tried converting both to int but that wont work either as that will conversion from unsigned short* to int will make u loose data, i know the theory behind my problem, help me out with the solution , if there is one. My problem is that this is realllllllllly big program and the input is an unsigned short* NOT unsigned short, and i cant convert it either. and i need a way to convert unsigned short* to char *, not from unsigned short to char*.
please kindly let me know if u know how to do it.
thnx,
srikant.
0
 
LVL 6

Expert Comment

by:gj62
ID: 8037001
srikant

1) You won't lose data when going from an unsigned short, or and unsigned short * to an int - an int is larger.  But neither of us suggested doing so.

2) An unsigned short * is a pointer to an unsigned short.  There is NO difference between the following:

unsigned short * pShort;
unsigned short sShort;

*pShort == sShort; - these will equate if the values are the same.

Both my code or zebada's will work, neither will change the value of the short pointed to or not be able to hold it...

0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8038411
guess u need this ( while in sprintf .. may be %d is not good for unsigned short .. i cant recall if there is any specific for short ..u shud uuse that .. for unsigned it is
%u);


#include <stdio.h>
#include <stdlib.h>
int
main()
{
 unsigned short v=8030;
 unsigned short *u=&v;
 char *s="8030";
char uu[6]; // maximum length possible for a string containing value of and unsigned short

sprintf(uu,"%d",*u);
if(!strcmp(uu,s))
printf("same (%d (number) == %s (string))\n",v,s);

}
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8038472
so stupid of me .. actually i would recommend zebada's method .. my method will also work .. but  i wud recommend his/(her)
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8038536
>>>i need a way to convert unsigned short* to char *, not from unsigned short to char*.

for that purpose only i gave u my 'stupid' solution
0
 

Author Comment

by:srik18
ID: 8039169
hey ppl,
thnx to gi62. i had done it in the right way initially but due to some problem in another segment thot the error was this. anyways as zebada was the first to respond, i am accepting his answer.
thnx to all.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question