字符串传换成16进制数组
怎么吧一个字符串转换成16进制
比如吧一个“2ad5f6e5”转换成16进制保存到unsigned char hex[4]中
[解决办法]
char sz[] = "2ad5f6e5";
unsigned char hex[4];
size_t len = strlen(sz);
size_t j = 0;
BYTE c;
for (size_t i = 0; i < len; i += 2)
{
c = sz[i];
if (c >= '0' && c <= '9') hex[j] = (c - '0') << 4;
else if (c >= 'a' && c <= 'f') hex[j] = (c - 'a' + 10) << 4;
c = sz[i + 1];
if (c >= '0' && c <= '9') hex[j++]
[解决办法]
= (c - '0');
else if (c >= 'a' && c <= 'f') hex[j++]
[解决办法]
= (c - 'a' + 10);
}