我无法理解如何在C中将十六进制字符串转换为unsigned char?

时间:2017-04-04 09:26:40

标签: c type-conversion hex unsigned-char

我有输入

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

有可能吗?

3 个答案:

答案 0 :(得分:3)

转换它的一种方法是以下方式:

  • 遍历字符串中的每个项目,其中每2个字符代表一个数字。
  • 分解每个此类字符对并将其存储在临时字符串中:char tmp {str[i], str[i+1], '\0'};
  • 在此字符串上调用strtol(tmp, NULL, 16)以获取整数。

答案 1 :(得分:1)

没有&#34;高水平&#34;功能

#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;
}

答案 2 :(得分: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++;
    }