Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BinaryReader read 4 bytes and don't get expected result

Tags:

c#

binary

byte

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

like image 866
John Doe Avatar asked Dec 31 '25 00:12

John Doe


2 Answers

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.

like image 117
Mark Byers Avatar answered Jan 02 '26 12:01

Mark Byers


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();
like image 31
aps Avatar answered Jan 02 '26 13:01

aps