I'm trying to read in a binary file in C. I have the specification of the file format and everything. I can parse the file with a software called "010 Editor". This way I can check if the values I read in in my C program are correct.
The problem I have is parsing 32 bit integer values in C.
This is the pretty simple code:
Expand|Select|Wrap|Line Numbers
- int temp = 0;
- fread(&temp,sizeof(int),1,fp);
- printf("%i\t",temp);
E.g. :
binary file hex values: 8A 1A 32 01
integer value I get in C: 138
It should be : 20060810
-> 138 is the unsigned byte value of 8A. Am I just reading in 8A?
For the following example it works:
binary file hex values: E1 7A 7F 00
integer value I get in C: 8354529
It should be : 8354529
I also tried to read in the data bytewise from the file and combinig it to an integer by shifting bytewise.
Code:
Expand|Select|Wrap|Line Numbers
- int temp = 0;
- temp = getc(fp);
- temp |= getc(fp) << 8;
- temp |= getc(fp) << 16;
- temp |= getc(fp) << 24;
- printf("%i\t",temp);
I'm using the MS Visual Studio compiler on Windows.
I'm stuck with this problem for quite a while now and I would appreciate any help I can get.
Thank You!