我不明白如何在C中把十六进制字符串转换成无符号字符?

我不明白如何在C中把十六进制字符串转换成无符号字符?,c,type-conversion,hex,unsigned-char,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

我有意见

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

有可能吗?

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

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++;
    }
遍历字符串的每一项,其中每2个字符表示一个数字。 将每个这样的字符对分开,并将它们存储在一个临时字符串中:char tmp{str[i],str[i+1],'\0'};。 在此字符串上调用strtoltmp,NULL,16以获取整数。
转换此值的一种方法是以下方法:

遍历字符串的每一项,其中每2个字符表示一个数字。 将每个这样的字符对分开,并将它们存储在一个临时字符串中:char tmp{str[i],str[i+1],'\0'};。 在此字符串上调用strtoltmp,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;
}
编辑 另一个例子是

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

没有高级功能

#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;
}
编辑 另一个例子是

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


一种方法是使用带%x2X格式字符串的sscanf创建循环。这是您要找的吗?是指您使用标准函数还是使用自己的代码?@mary在输入中一次跳过2个字符从查看预期结果我觉得十六进制缺少尾随8。一种方法是创建循环,将sscanf与%x2X格式字符串一起使用,这就是您要寻找的吗?是指您使用标准函数还是使用自己的代码?@mary在输入中一次跳过2个字符从查看预期结果开始,我觉得十六进制缺少尾随的8。您的代码无法工作。。。我有20000多个这样的符号���������������������������@mary我的代码运行良好,正如您所看到的@LPs我很抱歉,是的,它运行良好。我犯了一个错误;非常感谢。你的代码不工作。。。我有20000多个这样的符号���������������������������@mary我的代码运行良好,正如您所看到的@LPs我很抱歉,是的,它运行良好。我犯了一个错误;非常感谢。tmp=*ptrHex-'a';这肯定是错误的。考虑TMP=*pTrHEX—‘A’+10;即使这样也有weaknesses@chux如果字母是大写的,你是说缺点吗?大写是一个问题。迂腐的角落是C没有指定A、b等是连续的。然而,a-f在ASCII中是连续的,99.99%以上的编码和99%以上的余数tmp=*ptrHex-'a';这肯定是错误的。考虑TMP=*pTrHEX—‘A’+10;即使这样也有weaknesses@chux如果字母是大写的,你是说缺点吗?大写是一个问题。迂腐的角落是C没有指定A、b等是连续的。然而,a-f在ASCII 99.99%以上的编码和99%以上的余数中是连续的