3
votes

I use BinaryReader to read a file and i've problem i can't solve. (c#)

I need to read 4 bytes. When i look at these bytes with my hex viewer it's 00 00 00 13. So i tried Int32 fLength = dbr.ReadInt32(); The result is 318767104 instead of 19 (what i expected and need). When i use byte[] fLength = dbr.ReadBytes(4); i can see that i've read the correct bytes [0] [0] [0] [19].

(i've the same problem with the folowing bytes)

How can i read these 4 bytes and get 19 as result.

Thanks in advance !

Robertico

2

2 Answers

6
votes

It's a little endian vs big endian problem: 318767104 = 0x13000000

From the documentation:

BinaryReader stores this data type in little endian format.

Jon Skeet's miscutil has a reader that allows you to choose big or little endian.

1
votes

for reading a binary file 4bytes together

byte[] byteArray = new byte[(int)(flstrm.Length)];
int a= System.BitConverter.ToInt32(byteArray, 0); //here 0 is the start index
lbl1.Text= a.toString();