How can I get decimal from string hexdecimal:
I have unsigned char* hexBuffer = "eb89f0a36e463d";.
And I have unsigned char* hex[5] ={'\\','x'};.
I copy from hexBuffer first two char "eb" to hex[2] = 'e'; hex[3] = 'b';.
Now i have string "\xeb" or "\xEB" inside hex.
As we all know 0xEB its ahexdecimal and we can convert to 235 decimal.
How can I convert "\xEB" to 235(int)?
(Thanks to jedwards)
My Answer (maybe it will be useful for someone):
/*only for lower case & digits*/
unsigned char hash[57] ="e1b026972ba2c787780a243e0a80ec8299e14d9d92b3ce24358b1f04";
unsigned char chr =0;
int dec[28] ={0}; int i = 0;int c =0;
while( *hash )
{
c++;
(*hash >= 0x30 && *hash <= 0x39) ? ( chr = *hash - 0x30) : ( chr = *hash - 0x61 + 10);
*hash++;
if ( c == 1) dec[i] = chr * 16; else{ dec[i] += chr; c = 0; dec[i++];}
}
Typically I see homebrew implementations of hex2dec functions look like:
Which displays:
I’ll leave it as an exercise for you to go from nibble to byte and then from byte to arbitrary length string.
Note: I only used
#includeandprintfto demonstrate the functionality of thehex2dec_nibblefunction. Its not necessary to use these.