Anyone knows how, or any libraries that can be used?
Thanks in advance!
char * base16Str="1234567F";
char * base256Str;
Anyone knows how, or any libraries that can be used?
Thanks in advance!
char * base16Str="1234567F";
char * base256Str;
This is a very easy conversion, because you simply need to map pairs of base-16 characters to each base-256 character. i.e.:
char256[n] = char16[2*n] | (char16[2*n+1] << 4);
where I'm assuming that char16[]
entries are in the range 0 to 15, i.e. you've already mapped them from '0'
-'9'
, 'a'
- 'f'
(or 'A'
- 'F'
).
An easy method is to use a table lookup:
const char digits[257] = "0123456789ABCDEFGHIJKLMNOPQRST" /* ... */;
Search the array for the character and use the index as the digit's value.
To get the printable digit, use the value as the index into the array.
BTW, the array is 257, not 256, to allocate space for the '\0' character, which is not a digit character, but pleases the string functions.