Cleanest way to convert a two character string like "4D" to it's hex-equivalent-number ?
-
I currently do:
_stscanf(string, L"%x", &number);
it doesn't seem like any of the atoi/l functions can handle hexa-decimal input, any suggestions for something cleaner, or is as clean as it gets ? :-)do this :
char str[2]; // stored with your two characters
unsigned char ucConvertedNumber,
MSB, // most significant bits
LSB; // less significant bitsLSB = ((str[1] >= '0') && (str[1] <= '9')) ? str[1] - '0' :
((str[1] >= 'a') && (str[1] <= 'f')) ? str[1] - 'a' + 10 :
((str[1] >= 'A') && (str[1] <= 'F')) ? str[1] - 'A' + 10 : -1; // this happens only if your string is not a correct hexa representation
// do some error checking here...MSB = ((str[0] >= '0') && (str[0] <= '9')) ? str[0] - '0' :
((str[0] >= 'a') && (str[0] <= 'f')) ? str[0] - 'a' + 10 :
((str[0] >= 'A') && (str[0] <= 'F')) ? str[0] - 'A' + 10 : -1; // this happens only if your string is not a correct hexa representation
// do some error checking here...MSB = MSB << 4;
ucConvertedNumber = MSB | LSB;
you've got it... you can nox do :
printf("Decimal form :%d\n"
"Hexadecimal form :%x\n",
ucConvertedNumber,
ucConvertedNumber);
TOXCCT >>> GEII power
-
I currently do:
_stscanf(string, L"%x", &number);
it doesn't seem like any of the atoi/l functions can handle hexa-decimal input, any suggestions for something cleaner, or is as clean as it gets ? :-) -
long strtol( const char *nptr, char **endptr, int base ); acString strtol(acString, NULL, 16); Papa while (TRUE) Papa.WillLove ( Bebe ) ;
-
ok for a function, but understand what it does because it isn't a standard function...
TOXCCT >>> GEII power
Nice joke.
God is Real, unless declared Integer.
-
Nice joke.
God is Real, unless declared Integer.