Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple casting question- Please help!

Status
Not open for further replies.

robbaggio

Programmer
Jun 11, 2002
31
US
i want to cast a char * to an int, but i cant get it to work.

say my char * is called temp and my int is tempint.

i tried tempint = (int)temp, but that gave me really large number. I tried tempint = temp - '0', but got compile error(must use c style or reinteprert). I tried tempinit = reinterpret_cast<int>(temp), but that didnt give me correct integer value either. Any ideas on what i am doing wrong or what else i couldtry?

thanks a lot
robbaggio
 
Casting a pointer to an int will return you some large number which is the starting address of the pointer. What exactly are you trying to do?
 
yeah, i realized that now.
so i retried it doing tempinit = (int)*temp, which should convert the value of the pointer, but it still isnt working right.

i.e. when temp = 25 and i did it this way it gave me tempinit = 50.

so i still need some help i guess

robbaggio
 
Well, that will convert the value of the first element the pointer points to. *temp is equivalent to temp[0].

I still don't understand what you're trying to do.
 
i am trying to parse out a command line using argv as the current 'word' of the command line. I need to convert several of the words (argv)s to integers. So when i get to that word, i need a casting statement, i.e. int cmd1 = (int)*argv. but i am not getting correct values. When argv = '25', cmd1 = 50, when argv = 40, cmd1 = 52.

so something is messed up

robbaggio
 
char* is an address of a byte or byte array. That is, temp points to the first byte of an array of characters. temp, a pointer, cannot be cast into an int. If you happen to have an int stored in the first two or four bytes (sizeof(int)) of temp[], then you can convert the data with

// casts char* to int* and then dereferences int*
tempint = *(int*)temp;

However, this procedure is not too robust owing to the possibility of ints having different word lengths on different platforms.

The following program illustrates the point.

#include <iostream.h>
#include <string.h>
void main () {

char* temp = &quot;abcd&quot;;
int tempint;

// casts char* to int* and then dereferences int*
tempint = *(int*)temp;

cout << &quot;temp = &quot; << temp <<&quot; &quot; << strlen(temp) << endl;
cout << &quot;tempint = &quot; << tempint <<&quot; &quot; << sizeof(tempint) << endl;
cout.setf(ios::hex);
cout << &quot;hex representation of tempint = &quot; << tempint << endl;
}

Output is:

temp = abcd 4
tempint = 1684234849 4
hex representation of tempint = 64636261
 
Oh, I see you are trying to interpret argv[]. That is easier -- just plain parsing. It is self-contained on the platform. Word length is not an issue. Just convert the sring to an int. No casting needed.

#include <stdlib.h>
tempint = atoi(temp);

You must have an actual int stored in temp or atoi will just return 0. Fancier things are possible if you want to test that the argv really is an int. That is another story.
 
excellent, the atoi conversion worked like a charm.

thanks for all your help guys

robbaggio
 
Ha! I just noticed that my program is wrong owing to windows using little endian word format. The bytes of the int stored in temp are reversed. Oh well.:) Glad atoi worked.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top