mow*_*ker 27 c# endianness binaryreader
我正在尝试通过使用程序读取所有不同的信息来提高我对STFS文件格式的理解.使用一个网站,其中包含哪些偏移包含哪些信息,我写了一些有二进制阅读器的代码遍历文件并将值放在正确的变量中.
问题是所有数据都被支持为Big Endian,二进制读取器读取的所有内容都是Little Endian.那么,解决这个问题的最佳方法是什么?
我可以创建一个二进制读取器的模拟类,它返回一个反向的字节数组吗?有什么我可以在类实例中更改它将使它以大端读取,所以我不必重写所有内容?
任何帮助表示赞赏.
编辑:我尝试添加Encoding.BigEndianUnicode作为参数,但它仍然读取小端.
mow*_*ker 37
我通常不会回答我自己的问题,但我用一些简单的代码完成了我想要的东西:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我知道这就是我想要的,但我不知道如何写它.我找到了这个页面,它有所帮助:http://www.codekeep.net/snippets/870c4ab3-419b-4dd2-a950-6d45beaf1295.aspx
小智 11
恕我直言,这是一个稍微好一点的答案,因为它不需要新建一个不同的类,使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;
}
}
Run Code Online (Sandbox Code Playgroud)
BinaryReader与大多数这些答案不同,一个几乎完整的(出于我的目的)插入式替换可以正确处理字节序。默认情况下,它的工作方式与 完全相同BinaryReader,但可以构造为以所需的字节序读取。此外,这些Read<Primitive>方法被重载以允许您指定读取特定值的字节序 - 在您处理混合 LE/BE 数据流的(不太可能)场景中很有用。
public class EndiannessAwareBinaryReader : BinaryReader
{
public enum Endianness
{
Little,
Big,
}
private readonly Endianness _endianness = Endianness.Little;
public EndiannessAwareBinaryReader(Stream input) : base(input)
{
}
public EndiannessAwareBinaryReader(Stream input, Encoding encoding) : base(input, encoding)
{
}
public EndiannessAwareBinaryReader(Stream input, Encoding encoding, bool leaveOpen) : base(input, encoding, leaveOpen)
{
}
public EndiannessAwareBinaryReader(Stream input, Endianness endianness) : base(input)
{
_endianness = endianness;
}
public EndiannessAwareBinaryReader(Stream input, Encoding encoding, Endianness endianness) : base(input, encoding)
{
_endianness = endianness;
}
public EndiannessAwareBinaryReader(Stream input, Encoding encoding, bool leaveOpen, Endianness endianness) : base(input, encoding, leaveOpen)
{
_endianness = endianness;
}
public override short ReadInt16() => ReadInt16(_endianness);
public override int ReadInt32() => ReadInt32(_endianness);
public override long ReadInt64() => ReadInt64(_endianness);
public override ushort ReadUInt16() => ReadUInt16(_endianness);
public override uint ReadUInt32() => ReadUInt32(_endianness);
public override ulong ReadUInt64() => ReadUInt64(_endianness);
public short ReadInt16(Endianness endianness) => BitConverter.ToInt16(ReadForEndianness(sizeof(short), endianness));
public int ReadInt32(Endianness endianness) => BitConverter.ToInt32(ReadForEndianness(sizeof(int), endianness));
public long ReadInt64(Endianness endianness) => BitConverter.ToInt64(ReadForEndianness(sizeof(long), endianness));
public ushort ReadUInt16(Endianness endianness) => BitConverter.ToUInt16(ReadForEndianness(sizeof(ushort), endianness));
public uint ReadUInt32(Endianness endianness) => BitConverter.ToUInt32(ReadForEndianness(sizeof(uint), endianness));
public ulong ReadUInt64(Endianness endianness) => BitConverter.ToUInt64(ReadForEndianness(sizeof(ulong), endianness));
private byte[] ReadForEndianness(int bytesToRead, Endianness endianness)
{
var bytesRead = ReadBytes(bytesToRead);
if ((endianness == Endianness.Little && !BitConverter.IsLittleEndian)
|| (endianness == Endianness.Big && BitConverter.IsLittleEndian))
{
Array.Reverse(bytesRead);
}
return bytesRead;
}
}
Run Code Online (Sandbox Code Playgroud)
我不熟悉STFS,但改变endianess相对容易."网络订单"是一个大端,所以您需要做的就是从网络转换为主机订单.
这很容易,因为已经有代码可以做到这一点.看看IPAddress.NetworkToHostOrder,如下所述:ntohs()和ntohl()等价?
我认为您要小心执行此操作。想要从BigEndian转换为LittleEndian的原因是,如果要读取的字节在BigEndian中,并且根据它们计算的操作系统在LittleEndian中运行。
C#不再是仅用于窗口的语言。使用Mono等端口,以及其他Microsoft平台,例如Windows Phone 7/8,Xbox 360 / Xbox One,Windwos CE,Windows 8 Mobile,Linux(带有MONO),苹果(带有MONO)等端口。 BigEndian,在这种情况下,如果您不进行任何检查就转换了代码,则可能会陷入困境。
BitConverter上已经有一个名为“ IsLittleEndian”的字段,您可以使用它来确定操作环境是否在LittleEndian中。然后,您可以有条件地进行反转。
这样,我实际上只是写了一些byte []扩展而不是创建一个大类:
/// <summary>
/// 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
/// </summary>
/// <param name="byteArray">The source array to get reversed bytes for</param>
/// <param name="startIndex">The index in the source array at which to begin the reverse</param>
/// <param name="count">The number of bytes to reverse</param>
/// <returns>A new array containing the reversed bytes, or a sub set of the array not reversed.</returns>
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;
}
Run Code Online (Sandbox Code Playgroud)
因此,请看以下示例代码:
byte[] fontBytes = byte[240000]; //some data loaded in here, E.G. 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
Run Code Online (Sandbox Code Playgroud)