C# – Big Endian的二进制阅读器?

我正在尝试通过使用程序读取所有不同的信息来提高我对STFS文件格式的理解。 使用一个网站参考哪些偏移包含哪些信息,我写了一些有二进制阅读器的代码遍历文件并将值放在正确的变量中。

问题是所有数据都被支持为Big Endian,二进制读取器读取的所有内容都是Little Endian。 那么,解决这个问题的最佳方法是什么?

我可以创建一个二进制读取器的模拟类,它返回一个反向的字节数组吗? 有什么我可以在类实例中更改它将使它以大端读取,所以我不必重写所有内容?

任何帮助表示赞赏。

编辑:我尝试添加Encoding.BigEndianUnicode作为参数,但它仍然读取小端。

我通常不会回答我自己的问题,但我用一些简单的代码完成了我想要的东西:

class BinaryReader2 : BinaryReader { public BinaryReader2(System.IO.Stream stream) : base(stream) { } public override int ReadInt32() { var data = base.ReadBytes(4); Array.Reverse(data); return BitConverter.ToInt32(data, 0); } public Int16 ReadInt16() { var data = base.ReadBytes(2); Array.Reverse(data); return BitConverter.ToInt16(data, 0); } public Int64 ReadInt64() { var data = base.ReadBytes(8); Array.Reverse(data); return BitConverter.ToInt64(data, 0); } public UInt32 ReadUInt32() { var data = base.ReadBytes(4); Array.Reverse(data); return BitConverter.ToUInt32(data, 0); } } 

我知道这就是我想要的,但我不知道如何写它。 我找到了这个页面,它有所帮助: http : //www.codekeep.net/snippets/870c4ab3-419b-4dd2-a950-6d45beaf1295.aspx

恕我直言,这是一个稍微好一点的答案,因为它不需要新建一个不同的类,使big-endian调用显而易见,并允许大端和小端调用在流中混合。

 public static class Helpers { // Note this MODIFIES THE GIVEN ARRAY then returns a reference to the modified array. public static byte[] Reverse(this byte[] b) { Array.Reverse(b); return b; } public static UInt16 ReadUInt16BE(this BinaryReader binRdr) { return BitConverter.ToUInt16(binRdr.ReadBytesRequired(sizeof(UInt16)).Reverse(), 0); } public static Int16 ReadInt16BE(this BinaryReader binRdr) { return BitConverter.ToInt16(binRdr.ReadBytesRequired(sizeof(Int16)).Reverse(), 0); } public static UInt32 ReadUInt32BE(this BinaryReader binRdr) { return BitConverter.ToUInt32(binRdr.ReadBytesRequired(sizeof(UInt32)).Reverse(), 0); } public static Int32 ReadInt32BE(this BinaryReader binRdr) { return BitConverter.ToInt32(binRdr.ReadBytesRequired(sizeof(Int32)).Reverse(), 0); } public static byte[] ReadBytesRequired(this BinaryReader binRdr, int byteCount) { var result = binRdr.ReadBytes(byteCount); if (result.Length != byteCount) throw new EndOfStreamException(string.Format("{0} bytes required from stream, but only {1} returned.", byteCount, result.Length)); return result; } } 

我不熟悉STFS,但改变endianess相对容易。 “网络订单”是一个大端,所以您需要做的就是从网络转换为主机订单。

这很容易,因为已有代码可以做到这一点。 查看IPAddress.NetworkToHostOrder ,如下所述: ntohs()和ntohl()等价?

在我看来,你要小心这样做。 人们想要从BigEndian转换为LittleEndian的原因是,正在读取的字节是否在BigEndian中,并且针对它们的OS计算是在LittleEndian中运行的。

C#不再是一种仅限窗口的语言。 使用像Mono这样的端口,以及其他微软平台,如Windows Phone 7/8,Xbox 360 / Xbox One,Windwos CE,Windows 8移动版,带MONO的Linux,带有MONO的Apple等等。很可能操作平台可以在BigEndian,在这种情况下,如果你在没有进行任何检查的情况下转换代码,你就会搞砸自己。

BitConverter上已经有一个名为“IsLittleEndian”的字段,您可以使用它来确定操作环境是否在LittleEndian中。 然后你可以有条件地进行倒车。

因此,我实际上只是编写了一些byte []扩展而不是创建一个大类:

  ///  /// Get's a byte array from a point in a source byte array and reverses the bytes. Note, if the current platform is not in LittleEndian the input array is assumed to be BigEndian and the bytes are not returned in reverse order ///  /// The source array to get reversed bytes for /// The index in the source array at which to begin the reverse /// The number of bytes to reverse /// A new array containing the reversed bytes, or a sub set of the array not reversed. public static byte[] ReverseForBigEndian(this byte[] byteArray, int startIndex, int count) { if (BitConverter.IsLittleEndian) return byteArray.Reverse(startIndex, count); else return byteArray.SubArray(startIndex, count); } public static byte[] Reverse(this byte[] byteArray, int startIndex, int count) { byte[] ret = new byte[count]; for (int i = startIndex + (count - 1); i >= startIndex; --i) { byte b = byteArray[i]; ret[(startIndex + (count - 1)) - i] = b; } return ret; } public static byte[] SubArray(this byte[] byteArray, int startIndex, int count) { byte[] ret = new byte[count]; for (int i = 0; i < count; ++i) ret[0] = byteArray[i + startIndex]; return ret; } 

想象一下这个示例代码:

 byte[] fontBytes = byte[240000]; //some data loaded in here, EG a TTF TrueTypeCollection font file. (which is in BigEndian) int _ttcVersionMajor = BitConverter.ToUint16(fontBytes.ReverseForBigEndian(4, 2), 0); //output _ttcVersionMajor = 1 //TCCHeader is version 1