C语言中16进制字符串转为字节流的实现

void HexStrToByte(const char* source, unsigned char* dest, int length)
{
    short i;
    unsigned char highByte, lowByte;

    for (i = 0; i < length; i += 2)
    {
        highByte = toupper(source[i]);
        lowByte  = toupper(source[i + 1]);

        if (highByte > 0x39)
            highByte -= 0x37;
        else
            highByte -= 0x30;

        if (lowByte > 0x39)
            lowByte -= 0x37;
        else
            lowByte -= 0x30;

        // dest[i / 2] = (highByte << 4) | lowByte;
        dest[(length-2-i)/2] = (highByte << 4) | lowByte;
    }
    return;
}

你可能感兴趣的:(C语言)