Hello.
I am currently reteaching myself C (early stages of learning yet) and I am having an issue with printing the values of an integer in Hex. In the code below, it works correctly until i get to 128 then it decides to print a few f's in front of the the first byte of the hex.
Example print out should look like
FF 00 00 00
but it prints
FFFFFFFF 00 00 00.
Is there an issue in my pointer? it tells me there is a bad ptr when I debug it, but i don't know what is bad about it. Here is my code. Any tips would be appreciated.
Code:
#include <stdio.h>
#include <stdlib.h>
int i = 0;
void print_hex_signed_unsigned (int i)
{
char*
hex_ptr = (char*)&i;
printf("%02x ", hex_ptr[0]);
printf("%02x ", hex_ptr[1]);
printf("%02x ", hex_ptr[2]);
printf("%02x\t", hex_ptr[3]);
printf("%14d\t", i);
printf("%14u\n", i);
}
int main ()
{
int index;
for (index = 0; index < 256; index++)
print_hex_signed_unsigned(index * 1);
printf("press enter to continue");
getchar();
return(0);
}