#include <windows.h>
typedef unsigned (*h2d_fn)(const char *);
#define SEVEN_ZEROS 0, 0, 0, 0, 0, 0, 0
#define TWENTY_SIX_ZEROS \
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
#define FOURTY_EIGHT_ZEROS \
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
#define H2D_LOOKUP(x) { FOURTY_EIGHT_ZEROS,\
0, 0x1<<x, 0x2<<x, 0x3<<x, 0x4<<x, 0x5<<x, 0x6<<x, 0x7<<x, 0x8<<x, 0x9<<x,\
SEVEN_ZEROS,\
0xa<<x, 0xb<<x, 0xc<<x, 0xd<<x, 0xe<<x, 0xf<<x, TWENTY_SIX_ZEROS,\
0xa<<x, 0xb<<x, 0xc<<x, 0xd<<x, 0xe<<x, 0xf<<x\
}
static const unsigned h2d_tab0[103] = H2D_LOOKUP(0);
static const unsigned h2d_tab4[103] = H2D_LOOKUP(4);
static const unsigned h2d_tab8[103] = H2D_LOOKUP(8);
static const unsigned h2d_tab12[103] = H2D_LOOKUP(12);
static const unsigned h2d_tab16[103] = H2D_LOOKUP(16);
static const unsigned h2d_tab20[103] = H2D_LOOKUP(20);
static const unsigned h2d_tab24[103] = H2D_LOOKUP(24);
static const unsigned h2d_tab28[103] = H2D_LOOKUP(28);
static void write_string(const char * s);
static void write_decimal(unsigned u);
static char * read_string(char * buf, unsigned maxsize, unsigned * len);
static unsigned h2d_0(const char * s)
{
return 0;
}
static unsigned h2d_1(const char * s)
{
return h2d_tab0[*s];
}
static unsigned h2d_2(const char * s)
{
return h2d_tab4[*s] + h2d_tab0[*(s+1)];
}
static unsigned h2d_3(const char * s)
{
return
h2d_tab8[*s] + h2d_tab4[*(s+1)] + h2d_tab0[*(s+2)];
}
static unsigned h2d_4(const char * s)
{
return
h2d_tab12[*s] + h2d_tab8[*(s+1)] +
h2d_tab4[*(s+2)] + h2d_tab0[*(s+3)];
}
static unsigned h2d_5(const char * s)
{
return
h2d_tab16[*s] + h2d_tab12[*(s+1)] +
h2d_tab8[*(s+2)] + h2d_tab4[*(s+3)] + h2d_tab0[*(s+4)];
}
static unsigned h2d_6(const char * s)
{
return
h2d_tab20[*s] + h2d_tab16[*(s+1)] +
h2d_tab12[*(s+2)] + h2d_tab8[*(s+3)] +
h2d_tab4[*(s+4)] + h2d_tab0[*(s+5)];
}
static unsigned h2d_7(const char * s)
{
return
h2d_tab24[*s] + h2d_tab20[*(s+1)] +
h2d_tab16[*(s+2)] + h2d_tab12[*(s+3)] +
h2d_tab8[*(s+4)] + h2d_tab4[*(s+5)] + h2d_tab0[*(s+6)];
}
static unsigned h2d_8(const char * s)
{
return
h2d_tab28[*s] + h2d_tab24[*(s+1)] +
h2d_tab20[*(s+2)] + h2d_tab16[*(s+3)] +
h2d_tab12[*(s+4)] + h2d_tab8[*(s+5)] +
h2d_tab4[*(s+6)] + h2d_tab0[*(s+7)];
}
static h2d_fn h2d[9] = {
h2d_0, h2d_1, h2d_2, h2d_3, h2d_4, h2d_5, h2d_6, h2d_7, h2d_8
};
int main(void)
{
char buffer[9];
unsigned len;
write_string("TEST! hex 23DA should be decimal 9178 : ");
write_string("23DA = ");
write_decimal(h2d[4]("23DA"));
write_string("\n");
write_string("Enter a base-16 number (8 digits max) = ");
read_string(buffer, 8, &len);
write_string("Converted to base-10 = ");
write_decimal(h2d[len](buffer));
write_string("\n");
return 1;
}
static void write_string(const char * s)
{
const char * p = s;
DWORD count = 0;
while (*p++ != '\0') ++count;
WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), s, count, &count, NULL);
}
static void write_decimal(unsigned u)
{
static char buf[11];
char * p = buf + 10;
if (u == 0) {
write_string("0");
return;
}
*p = '\0';
do {
*--p = (u % 10) + '0';
u /= 10;
}
while (u != 0);
write_string(p);
}
static char * read_string(char * buf, unsigned maxsize, unsigned * len)
{
HANDLE hin = GetStdHandle(STD_INPUT_HANDLE);
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD temp;
DWORD oldmode;
char * where = buf;
char * end = buf + maxsize;
int done = 0;
char space = ' ';
*len = 0;
GetConsoleMode(hin, &oldmode);
SetConsoleMode(hin, ENABLE_PROCESSED_INPUT);
while (done == 0) {
ReadConsole(hin, where, 1, &temp, NULL);
if ((*where == 13) || (*where == 10) || (*where == 26)) done = 1;
else {
if (*where == 8) {
if (*len > 0) {
WriteConsole(hout, where, 1, &temp, NULL);
WriteConsole(hout, &space, 1, &temp, NULL);
WriteConsole(hout, where, 1, &temp, NULL);
--where;
--(*len);
}
}
else {
if (*where == 9) *where = space;
WriteConsole(hout, where, 1, &temp, NULL);
if (where != end) {
++(*len);
++where;
}
}
}
}
SetConsoleMode(hin, oldmode);
*where = '\0';
write_string("\n");
return buf;
}