// Shortest function to convert an ascii hex digit to binary
// (assumes a valid hex digit 0-9, A-F, a-f)

char asciihex2bin(char ascii)
{
  if (ascii > 0x40) ascii += 9;
  return (ascii & 0x0F);
}

Did you mean ... a short hex char to int conversion ?

// ascii_hex_char_2_int.cpp //

// http://developers-heaven.net/forum/index.php/topic,46.0.html

#include <iostream>
#include <string>

using namespace std;

/*
48  060 30  00110000    0   
49  061 31  00110001    1   
50  062 32  00110010    2   
51  063 33  00110011    3   
52  064 34  00110100    4   
53  065 35  00110101    5   
54  066 36  00110110    6   
55  067 37  00110111    7   
56  070 38  00111000    8   
57  071 39  00111001    9

65  101 41  01000001    A   
66  102 42  01000010    B   
67  103 43  01000011    C   
68  104 44  01000100    D   
69  105 45  01000101    E   
70  106 46  01000110    F   

97  141 61  01100001    a   
98  142 62  01100010    b   
99  143 63  01100011    c   
100 144 64  01100100    d   
101 145 65  01100101    e   
102 146 66  01100110    f   
*/

// Shortest? function to convert an ascii hex digit (CHAR) 
// to *AN INTEGER* 0..15.
//
//      ( assumes a valid hex digit CHAR IN RANGE: 
//        '0'..'9', 'A'..'F', 'a'..'f' )
int asciihex2int( char ascii )
{
    if( ascii > 0x40 ) // HANDLES: 'A'..'F' and 'a' .. 'f'
        return (ascii & 0x0f) + 9 ; // (ascii & 0x0f) returns 1..6

    return ascii - '0'; // '0' .. '9'
}


bool more()// defaults to 'true'/'yes' ... unless 'n' or 'N' entered
{
    cout << "\nMore (y/n) ? " << flush;
    int reply = cin.get();
    cin.sync(); // 'flush' cin stream ...
    if( reply == 'n' || reply == 'N' ) return false;
    // else ...
    return true;
}


int main()
{
    string line;

    do
    {
        cout << "Get ascii hex char to convert to int: " << flush;
        string line;
        getline( cin, line );

        if( line.size() )
        {
            char c = toupper(line[0]);

            if( (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') )
            {
                cout << "asciihex2int( '" << c << "' ) = " 
                     << asciihex2int( c ) << endl;
            }
            else
                cout << endl << c << " is NOT valid input here ...\n";
        }
    }
    while( more() ); // make sure cin stream is 'empty' before calling more()

    cout << "\nPress 'Enter' to continue/exit ... " << flush;
    cin.get(); // keep 'Window' open until 'Enter' key is pressed ...
}

Edit: Oops ... this is C++ ... oh well ... easy to convert any

cout << int_x << endl;
to ...
printf( "%d\n", int_x );

and to use fgets with a big char buffer
(instead of using C++ string and getline, etc...)

please edit above line 78, 79 to ...

 cout << "asciihex2int( '" << line[0] << "' ) = "
      << asciihex2int( line[0] ) << endl;

so that the 'original char' is used there.

Sure, there are a lot of smart guys around. But if you think you've got a good idea, this should not deter you from publishing it, I guess...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.