Your function should be:
void show_bytes (byte_pointer x) { int i; for(i = 0; i <sizeof(float); i++) { printf("0x%2X\n", (unsigned int)(*(x++) & 0xFF)); } }
or
typedef uint8_t *byte_pointer; void show_bytes (byte_pointer x) { int i; for(i = 0; i <sizeof(float); i++) { printf("0x%2X\n", *(x++)); } }
In your code, the problem is that the type of the signed a pointer is raised to signed int by printf .
Format
%2X does not limit the output digit; it tells only printf that the result string must be at least 2 characters long.
- First solution: the value is increased to
signed int , but the past value is truncated in LSB. - Second example: the value is truncated by the type of the pointer, which is
unsigned char .
Rule: in raw memory, always use unsigned types.
source share