0

I want to read data from a .bin file. Actually If I preview the data of my bin file I see something like this:

0000000    3030    3030    3030    3730    300a    3030    3030    3030
0000010    0a35    3330    3030    3030    3030    300a    3031    3030

So I just want to read first the first 2 32-bit signed int.

My code is this:

int data,data2;

fread(&data,4,1, ptr_myfile);
printf("First Data read in hex is: %x\n",data);

/*read the second 32 bit integer*/
fread(&data2,sizeof(int),1, ptr_myfile);
printf("Second data read in hex is: %x\n",data2);

My output is this:

First Data read in hex is: 30303030
Second data read in hex is: 37303030
  1. So my first question is why they are read in this order? and the second one is not 30303730? and which one is the correct under the assumption that I have to read the first two signed 32 bit integers?

And more important

  1. The second number declares the rest 32 bit signed ints that should exist in the bin file. There are some notes that describe this bin file and I know that the second number should be equal to 4, or a little bit bigger but at no case 37303030 which is extremely large number.

I think there is something wrong with my conversion or the way I read the bin file.

The bin file is supposed to contain:

EDIT:

The bin file is ASCII text with UNIX-style line-endings. It
consist of a series of 32-bit signed integers in hexadecimal only

Any help on what am I missing here?

4

3 回答 3

1

When comparing multi-byte binaries in a raw format, you might have to consider Endianess. At least that's the first thing that comes to my mind when the number's bytes appear "reversed" when you display them after reading them and printing.

于 2013-05-17T22:38:31.763 回答
0

It's very likely what Henrik said, and using printf is adding additional murkiness to the waters.

Try this:

unsigned char data[4],data2[4];

fread(&data,4,1, ptr_myfile);
printf("First Data read in hex is: %x, %x, %x, %x\n",data[0], data[1], data[2], data[3]);

/*read the second 32 bit integer*/
fread(&data2,4,1, ptr_myfile);
printf("Second data read in hex is: %x, %x, %x, %x\n",data2[0], data2[1], data2[2], data2[3]);

This will give you the bytes as you read them, without any byteswapping due to endedness issues.

于 2013-05-17T22:44:15.183 回答
0

This does look like an encoding issue.

That file seems to contain plain ASCII values of the numbers - at least judging from the sample you gave us.

The ASCII-Hex-Code 30 is the character 0, 31=1,..., 39=9. That ASCII-Hex-Code 0A is a linefeed.

I would not be too surprised if you also found the values 41 until 46 which would resolve to A - F, or maybe 61 until 66 which would be the lowercase variants a - f.

As that description claims those number to be 32bit wide, I would suggest you try to read up to 8 characters (bytes) per number and convert those ASCII values into numeric values by e.g. using sscanf - as said, numbers most likely are delimited by that linefeed.

However, the confusing part is that your given sample contains numbers that are obviously made from more than 8 characters;

35 3330 3030 3030 3030 30

That would resolve to

5300 0000 00 which is more than 32bit wide and hence not fitting the description.

So something is wrong, either the description (which should say e.g. 64bit), or your quote (you somehow mixed things up while copying) or the entire file format is broken (less likely).

Well, I would start by parsing them line by line and trying to convert them to binary values by using sscanf monster.

于 2013-05-17T23:18:46.693 回答