0

for Pset4, recover, I'm trying to read 4 bytes from card.raw into a buffer. I''m testing the first 4 bytes of card.raw with what my buffer contains using gdb, and I'm not getting the same thing. I can't figure out why. Is there something wrong with how I'm using fread, malloc and/or the int*, buffer?

//creates buffer of 4 bytes 
 int* buffer = malloc(sizeof(int));

 //error 2 if buffer is null
if (buffer == NULL)
        {
            return 2;
            }

 //reads 4 ints from infile into buffer         
 fread(buffer, sizeof(int), 1, infile);

Gdb tells me that *buffer is 779381090 (in decimal I believe) each time. I expect the buffer to be 6269 742e (in hex).

Thank you!

2
  • Why do you expect the cards's firsts bytes to be 6269742e? Can you explain from where that value comes from? – wallek876 Jan 28 '15 at 20:24
  • I used xxd -l 4 card.raw to display the first 4 bytes in the terminal – Faith Jan 28 '15 at 20:27
2

The problem is that you are not printing using the same format in gdb and with the xxd command. xxd outputs hexadecimal and gdb outputs in decimal.

To print in the same format with both programs a solution can be this.

In the command line type:

xxd -g 1 -l 4 card.raw

Notice that -g 1 separetes the output of xxd in hexadecimals of one byte width, and the ouptput should be:

0000000: 62 69 74 2e

Insided gdb type:

x /4bx buffer

This prints in gdb the first four bytes of buffer in hexadecimal with one byte width, and the output should be something like this:

0x804b170:  0x62    0x69    0x74    0x2e

Now the outputs match.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .