Farshid.M
Farshid.M

Reputation: 389

How to convert char array to hexadecimal

I want to convert char array filled by user (like part1 in code below) to hexadecimal. In fact, convert two char to one hexadecimal char.

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

Now I can put part1 hex to char ans like this:

ans=0xf2;

Upvotes: 1

Views: 7744

Answers (2)

Anton Todua
Anton Todua

Reputation: 687

This is the possible solution:

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

UPDATE:

These are methods to convert char to it's hexadecimal digit:

// 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];
}

Upvotes: 1

Kerrek SB
Kerrek SB

Reputation: 477670

Step 1: Parse each textual character as a number:

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

Step 2: Use mathematics to assemble the result, following the rules of the place-value notation:

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

Upvotes: 3

Related Questions