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
And more important
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?
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.