SteveCRM
Feb 1st, 2001, 06:36 PM
I know there is a hex command in the windows.h file but is there a way to convert to binary and decimal?
HarryW
Feb 2nd, 2001, 03:07 AM
Well you don't really convert a number between radixes (not sure if that's the correct plural), you just convert the number to a string in different ways; all numbers are stored as binary after all.
To output a number to a radix R (a radix is a base number - hex is radix 16, decimal is radix 10, binary is radix 2, etc) you can use the itoa() function like this:
char buf[50]; // just a buffer for working in
int TheNumber = 64;
printf("TheNumber in decimal is: %s", itoa(TheNumber, buf, 10));
printf("TheNumber in hex is: %s", itoa(TheNumber, buf, 16));
printf("TheNumber in binary is: %s", itoa(TheNumber, buf, 2));
printf("TheNumber in octal is: %s", itoa(TheNumber, buf, 8));
Alternatively, if you're using C++ and cin / cout, you can #include <iomanip> and use the stream manipulators like 'hex' and 'dec':
cout << hex << TheNumber // prints TheNumber to radix 16