tags:

views:

103

answers:

2

Anyone knows how, or any libraries that can be used?

Thanks in advance!

char * base16Str="1234567F";
char * base256Str;
+1  A: 

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').

Oli Charlesworth
Is there an example? if I have char * base16Str="1234567F", and wishes to convert to char * base256Str;
seveleven
@seveleven: As I said, you need to map the characters to the range 0 to 15 first. A three-line helper function can do this (`if (c >= '0' `, etc.).
Oli Charlesworth
A: 

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.

Thomas Matthews