[英]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.