Of course, atoi() converts the leading zero for you.
Skip "0x" then use strtol() function.
It's so easy...
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
Regrettably you don't understand me.
char* str = "0x15";
int value;
char* junk;
...
value = strtol(str+2,&junk,16); /* str+2 <=> skip 0x */
...
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
C string is an array of char. Get this array size (length of string) then make a loop swapping leading and trailing chars ( s[0]<=>s[n-1], s[1]<=>s[n-2] and so on)...
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
Stop stop: now I don't understand you. Please, explain what do you want to do really?
If you want fill char value 0x15, use hex char literal '\x15' directly: what for this bustle with conversions of "0x15" string? If you want an array of 5 (or 8) char, declare it as char buffer[8]; - why operator new?
Please, use CODE tag for your snippets:[code=c]
your code
[/code]
We have the intruder in this thread; now one haveaclick has his/her own thread on the Global Problem of String Reversions topic...
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
Do you actually want to convert to little endian or is this some kind of homework project? Because converting to hexadecimal doesn't enter into the picture if you're working with the bytes. The representation only matters for display and such.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Well, since you want to convert to little endian, I'll assume you have something in big endian format already. If that's the case it's a simple matter of reversing the bytes. For example:
void bswap ( unsigned int x )
{
x = ( x >> 24 ) | ( ( x << 8 ) & 0x00FF0000 ) |
( ( x >> 8 ) & 0x0000FF00 ) | ( x << 24 );
}
Though ntohl and htonl are generally a better option if they're available.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401