[英]I can't understand how to convert hex string to unsigned char in C?
我有输入
unsigned char hex[64] = 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a0"
我想要这样的输出:
unsigned char tmpInHash[32] = { 0x9f, 0x86, 0xd0, 0x81, 0x88, 0x4c, 0x7d, 0x65, 0x9a, 0x2f, 0xea, 0xa0, 0xc5, 0x5a, 0xd0, 0x15, 0xa3, 0xbf, 0x4f, 0x1b, 0x2b, 0x0b, 0x82, 0x2c, 0xd1, 0x5d, 0x6c, 0x15, 0xb0, 0xf0, 0x0a, 0x08 }
我到处都在搜索答案,但是找到的答案不适合。
编辑
我在写的时候想要:
for(i=0; i< strlen(tmpInHash); i++ {
printf("%c ", tmpInHash);
}
我得到这个:
0x9f 0x86 0xd0 0x81 0x88 0x4c 0x7d 0x65 0x9a ...
可能吗?
一种转换方法是以下方法:
char tmp {str[i], str[i+1], '\\0'};
。 strtol(tmp, NULL, 16)
以获取整数。 没有“高级”功能
#include <stdio.h>
#include <string.h>
int main(void)
{
unsigned char hex[] = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08";
size_t stringLength = (sizeof(hex)/sizeof(hex[0]))-1;
unsigned char tmpInHash[stringLength/2];
int j=0;
// reset the char to 0. This grants that next or operation works on reset buffer
tmpInHash[0] = 0;
// loop that parse the whole string
for (size_t i = 0; i < stringLength; i++)
{
// check if the char is a to f
if ((hex[i] >= 'a') && (hex[i] <= 'f'))
{
tmpInHash[j] |= hex[i] -'a' + 10;
}
// che if is a digit
else if ((hex[i] >= '0') && (hex[i] <= '9'))
{
tmpInHash[j] |= hex[i] -'0';
}
// character not allowed
else
{
fprintf(stderr, "Character not allowed: %c position: %zu\n", hex[i], i);
return 1;
}
// even index chars are hig 4 bits of unsigned char
if ((i%2) == 0)
{
tmpInHash[j]<<=4;
}
else
{
// nex unsigned char
j++;
// reset the char to 0. This grants that next or operation works on reset buffer
if (j < stringLength/2)
tmpInHash[j] = 0;
}
}
for (size_t i = 0; i < stringLength/2; i++)
{
printf("0x%02X ", tmpInHash[i]);
}
printf("\n");
return 0;
}
输出值
0x9F 0x86 0xD0 0x81 0x88 0x4C 0x7D 0x65 0x9A 0x2F 0xEA 0xA0 0xC5 0x5A 0xD0 0x15 0xA3 0xBF 0x4F 0x1B 0x2B 0x0B 0x82 0x2C 0xD1 0x5D 0x6C 0x15 0xB0 0xF0 0x0A 0x08
编辑另一个例子可以是
#include <stdio.h>
#include <string.h>
unsigned char hex[] = "9f86d081884g7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08";
#define HEX_LEN (sizeof(hex)-1)
unsigned char tmpInHash[HEX_LEN/2]={0};
int main(void)
{
size_t i = 0;
size_t j = 0;
// parse all charcaters of hex
while(hex[i] != '\0')
{
// check if the char is a to f
if ((hex[i] >= 'a') && (hex[i] <= 'f'))
{
tmpInHash[j] |= hex[i] -'a' + 10;
}
// che if is a digit
else if ((hex[i] >= '0') && (hex[i] <= '9'))
{
tmpInHash[j] |= hex[i] -'0';
}
// character not allowed
else
{
fprintf(stderr, "Character not allowed: %c position: %zu\n", hex[i], i);
return 1;
}
// even index chars are hig 4 bits of unsigned char
if ((i%2) == 0)
{
tmpInHash[j]<<=4;
}
else
{
// nex unsigned char
j++;
}
// next hex char
i++;
}
// print loop
for (i = 0; i < (HEX_LEN/2); i++)
{
printf("0x%02X ", tmpInHash[i]);
}
printf("\n");
return 0;
}
unsigned char hex[64] = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a0";
unsigned char *ptrHex =hex;
unsigned char tmpInHash[32];
int i ,tmp ;
for(i=0 ; i < 32 ; i++)
{
if((*ptrHex)<=9 && (*ptrHex) >=0)
tmp=(*ptrHex)-'0';
else tmp=(*ptrHex)-'a'+10;
tmpInHash[i]=(tmp<<4);
ptrHex++;
if((*ptrHex)<=9 && (*ptrHex) >=0)
tmp=(*ptrHex)-'0';
else tmp=(*ptrHex)-'a'+10;
tmpInHash[i]|=tmp ;
ptrHex++;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.