Is there a library function that takes in an integer and converts it to a single-byte hexadecimal or binary number?
For example, if I passed it the input of
64, it would output
preguntado el 09 de marzo de 12 a las 15:03
For hex numbers, you can use
char buff; sprintf(buff, "0x%02x", 64);
An int is an int, whether it is 0x40 or 64; the data representation of the two is exactly the same (10000000...011111111). If you are asking how it would be represented in a char array, you'd use
sprintf. The simplest way is
sprintf(buf, "%#x", 64).
Internally, integers are already represented as binary. You can la visualización a number as hexadecimal using the
%x format string (
%#02x will fit your example best).
Vea esta pregunta regarding binary, for which there isn't a built-in format string specifier.
En C el int type's size depends upon implementation. Normally, it will be 4 bytes long, and thus impossible to storing in a single byte without losing important information.
Si utiliza un
int8_t then you will have a single byte. Bytes are binary internally and always will be. So anytime you want to do cualquier cosa with your byte, you must do it in binary.
Hexadecimal vs binary vs base 10 is a display decision. So if you accept those as input, you will have to convert a string into a single byte for storage in memory. When you display them, you will have to convert to the desired display format.