Reputation: 919
I want to convert an 32-bit ASCII number (e.g. "FE257469") to the equivalent 32-bit hex number which will be stored on a 32-bit variable. Most importnatly, I want to do that without using any library function like sscanf(), atoi(), etc.
Any ideas on that?
Thank in advance.
Upvotes: 0
Views: 4291
Reputation: 14077
Here is an implementation of such a function based on a switch:
unsigned int parseHex( char *str )
{
unsigned int value = 0;
for(;; ++str ) switch( *str )
{
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
value = value << 4 | *str & 0xf;
break;
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
value = value << 4 | 9 + *str & 0xf;
break;
default:
return value;
}
}
Upvotes: 1
Reputation: 490148
The usual way is something like:
initialize result to 0
Upvotes: 1