Hex to char array in C

后端 未结 11 1136
广开言路
广开言路 2020-11-30 09:04

Given a string of hex values i.e. e.g. \"0011223344\" so that\'s 0x00, 0x11 etc.

How do I add these values to a char array?

Equivalent to say:

<
11条回答
  •  忘掉有多难
    2020-11-30 10:03

    {
        char szVal[] = "268484927472";
        char szOutput[30];
    
        size_t nLen = strlen(szVal);
        // Make sure it is even.
        if ((nLen % 2) == 1)
        {
            printf("Error string must be even number of digits %s", szVal);
        }
    
        // Process each set of characters as a single character.
        nLen >>= 1;
        for (size_t idx = 0; idx < nLen; idx++)
        {
            char acTmp[3];
            sscanf(szVal + (idx << 1), "%2s", acTmp);
            szOutput[idx] = (char)strtol(acTmp, NULL, 16);
        }
    }
    

提交回复
热议问题