?
Solved

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

Posted on 2003-02-27
9
Medium Priority
?
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Suggested Courses

765 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