So I am writing a very small and simple program which takes a number as input, converts it to hex, and prints it out two characters at a time.
For some numbers, it prints out ffffff in front of the output.
This is my code:
//Convert the input to an unsigned int
unsigned int a = strtoul (argv[1], NULL, 0);
//Convert the unsigned int to a char pointer
char* c = (char*) &a;
//Print out the char two at a time
for(int i = 0; i < 4; i++){
printf("%02x ", c[i]);
}
Most of the output is fine and looks like this:
./hex_int 1
01 00 00 00
But for some numbers the output looks like this:
./hex_int 100000
ffffffa0 ffffff86 01 00
If you remove all the f's the conversion is correct, but I cannot figure out why it is doing this only on some inputs.
Anyone have any ideas?