2011-10-08 62 views
3

我使用BinaryReader来读取文件,我有问题,我无法解决。 (c#)BinaryReader读取4个字节,并没有得到预期的结果

我需要读取4个字节。当我用十六进制查看器查看这些字节时,它是00 00 00 13。 所以我试了Int32 fLength = dbr.ReadInt32();结果是318767104而不是19(我的期望和需要)。当我使用byte[] fLength = dbr.ReadBytes(4);时,我可以看到我已经阅读了正确的字节[0] [0] [0] [19]。

(我同样的问题与如下因素字节)

我如何可以读取这些4个字节,并得到19结果。

在此先感谢!

Robertico

回答

6

这是一个little endian vs big endian问题:318767104 = 0x13000000

documentation

BinaryReader在商店用little endian格式这种数据类型。

Jon Skeet的miscutil有一个阅读器,允许您选择大或小的字节序。

+0

哦男孩。我应该知道的。谢谢 ! –

+0

分享我选择的解决方案:http://www.csharp-examples.net/reverse-bytes/ –

1

用于读取二进制文件一起4字节

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