-
hex to dec to binary
Hi!
Can anyone tell me how to convert from hex to binary(8bit and 16 bit).
Also I want a convertsion from dec to binary.
Binary back to hex.
I am writing an assembler and I need some function to accomplish this.Are there any c library functions?
Thanks in advance.
-
You can use ultoa to get them into string format.
http://www.cplusplus.com/ref/cstdlib/ultoa.html
I'm not aware of any library functions that do the conversions directly. They wouldn't be too hard to write yourself though.
-
You can use sprintf or printf to convert to a hex string, and strtoul or strtol to convert a hex string to an int.
Code:
unsigned int num = 0xffffffff;
char hex_str[33];
sprintf(hex_str, "%X", num);
printf("%s\n", hex_str);
num = strtoul(hex_str, NULL, 16);
printf("%X\n", num);
There's no standard library functions to convert a number to a binary string. It's easy to write your own by using bit shifting and masking.