如何将char数组转换为十六进制

时间:2015-11-29 12:41:21

标签: c hex

我想将用户填充的char数组(如下面的代码中的part1)转换为十六进制。实际上,将两个char转换为一个十六进制char

char part1[2];
char ans;
part1[0]='f';
part1[1]='2';

现在我可以将part1十六进制加到char ans,如下所示:

ans=0xf2;

2 个答案:

答案 0 :(得分:2)

步骤1:将每个文本字符解析为数字:

unsigned int parse_char(char c)
{
    if ('0' <= c && c <= '9') return c - '0';
    if ('a' <= c && c <= 'f') return 10 + c - 'a';
    if ('A' <= c && c <= 'F') return 10 + c - 'A';

    abort();
}

步骤2:使用数学来组合结果,遵循地方价值符号的规则:

unsigned char ans = parse_char(part1[0]) * 0x10 + parse_char(part1[1]);

答案 1 :(得分:1)

这是可能的解决方案:

unsigned char CharToHex( char c )
{
    switch( c ) {
        case '0': return 0;
        case '1': return 1;
        case '2': return 2;
        case '3': return 3;
        case '4': return 4;
        case '5': return 5;
        case '6': return 6;
        case '7': return 7;
        case '8': return 8;
        case '9': return 9;
        case 'a': case 'A': return 10;
        case 'b': case 'B': return 11;
        case 'c': case 'C': return 12;
        case 'd': case 'D': return 13;
        case 'e': case 'E': return 14;
        case 'f': case 'F': return 15;
    }
    return 16;
}

// return 0 if error occurred
int Convert( const char* source, char* dest )
{
    for( int i = 0; source[i] != '\0' && source[i + 1] != '\0'; i += 2 ) {
        unsigned char  hex1 = CharToHex( source[i] );
        unsigned char  hex2 = CharToHex( source[i + 1] );
        if( hex1 == 16 || hex2 == 16 ) {
            return 0;
        }
        *dest = ( char )( hex1 << 4 | hex2 );
        dest++;
    }
    return 1;
}

更新:

这些是将char转换为十六进制数字的方法:

// variant 1
unsigned char CharToHex( char c )
{
    if( c >= '0' && c <= '9' ) {
        return ( c - '0' );
    }
    if( c >= 'a' && c <= 'f' ) {
        return ( c - 'a' + 10 );
    }
    if( c >= 'A' && c <= 'F' ) {
        return ( c - 'A' + 10 );
    }
    return 16;
}
// variant 2
unsigned char CharToHex( char c )
{
    c = tolower( c );
    if( c >= '0' && c <= '9' ) {
        return ( c - '0' );
    }
    if( c >= 'a' && c <= 'f' ) {
        return ( c - 'a' + 10 );
    }
    return 16;
}
// variant 3
unsigned char CharHexConversionTable[256] = {
    // ascii
    16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
    16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
    16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
     0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 16, 16, 16, 16, 16, 16,
    16, 10, 11, 12, 13, 14, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16,
    16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
    16, 10, 11, 12, 13, 14, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16,
    16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
    // rest
    16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
    16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
    16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
    16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
    16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
    16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
    16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
    16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16
};
unsigned char CharToHex( char c )
{
    return CharHexConversionTable[(unsigned char)c];
}