开发者

Convert base 16 string to base 256 string in C++, vice versa

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜