Tuesday, May 31, 2005

C++ code: Decimal to Hex and viceversa

Decimal to Hex
====================
static
char* decimalToHex(int dec)
{
static char hexStr[2];
char *str="0123456789ABCDEF";
hexStr[1] = str[(dec & 15)];
hexStr[0] = str[(dec >> 4)];
return hexStr;
}


Hex to Decimal
=======================

static int hexToDec(char chr)
{
char* str = "0123456789ABCDEF";
char modifiedChar = toupper(chr);
int index = -1;
int i=0;
for (i = 0; i < 16;i++)
{
if (str[i] == modifiedChar)
{
index=i;
break;
}
}
return index;
}

0 Comments:

Post a Comment

<< Home