2011-10-08 27 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();
相關問題