srik18
asked on
how to convert an 'unsigned short *' variable to 'char *'
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
ASKER
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.
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.
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...
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...
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);
}
%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);
}
so stupid of me .. actually i would recommend zebada's method .. my method will also work .. but i wud recommend his/(her)
>>>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
for that purpose only i gave u my 'stupid' solution
ASKER
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.
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.
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");
}