如何才能简化网络字节顺序从BinaryReader在一个转换?字节、顺序、网络、BinaryReader

2023-09-02 10:51:41 作者:一见你就笑

System.IO.BinaryReader在小尾数格式读取值。

System.IO.BinaryReader reads values in a little-endian format.

我有一个C#应用程序连接到服务器端的专有网络库。服务器端的网络字节顺序发送都记录下来,正如人们所期望的,但我觉得这个处理在客户端的尴尬,特别是对无符号值。

I have a C# application connecting to a proprietary networking library on the server side. The server-side sends everything down in network byte order, as one would expect, but I find that dealing with this on the client side is awkward, particularly for unsigned values.

UInt32 length = (UInt32)IPAddress.NetworkToHostOrder(reader.ReadInt32());

是我想出来的,以获得正确的无符号数出流的唯一途径,但这样显得既尴尬又丑,而且我还没有测试,如果这只是要剪掉高位值,所以我所要做的乐趣BitConverter的东西。

is the only way I've come up with to get a correct unsigned value out of the stream, but this seems both awkward and ugly, and I have yet to test if that's just going to clip off high-order values so that I have to do fun BitConverter stuff.

有没有一些方法,我很想念短写一个包装围绕整个事情,以避免在每次阅读这些丑陋的转换呢?好像应该对读者的字节序选项,以使这样的事情简单一些,但是我还没有碰到过什么。

Is there some way I'm missing short of writing a wrapper around the whole thing to avoid these ugly conversions on every read? It seems like there should be an endian-ness option on the reader to make things like this simpler, but I haven't come across anything.

推荐答案

有没有内置的转换器。这里是我的包装(如你所见,我只实现我所需要的功能,但结构是pretty的容易改变自己的喜好):

There is no built-in converter. Here's my wrapper (as you can see, I only implemented the functionality I needed but the structure is pretty easy to change to your liking):

/// <summary>
/// Utilities for reading big-endian files
/// </summary>
public class BigEndianReader
{
    public BigEndianReader(BinaryReader baseReader)
    {
        mBaseReader = baseReader;
    }

    public short ReadInt16()
    {
        return BitConverter.ToInt16(ReadBigEndianBytes(2), 0);
    }

    public ushort ReadUInt16()
    {
        return BitConverter.ToUInt16(ReadBigEndianBytes(2), 0);
    }

    public uint ReadUInt32()
    {
        return BitConverter.ToUInt32(ReadBigEndianBytes(4), 0);
    }

    public byte[] ReadBigEndianBytes(int count)
    {
        byte[] bytes = new byte[count];
        for (int i = count - 1; i >= 0; i--)
            bytes[i] = mBaseReader.ReadByte();

        return bytes;
    }

    public byte[] ReadBytes(int count)
    {
        return mBaseReader.ReadBytes(count);
    }

    public void Close()
    {
        mBaseReader.Close();
    }

    public Stream BaseStream
    {
        get { return mBaseReader.BaseStream;  }
    }

    private BinaryReader mBaseReader;
}

基本上,ReadBigEndianBytes做繁重的工作,这是传递给BitConverter。将有一个明确的问题,如果你读了大量的字节,因为这会导致大量的内存分配。

Basically, ReadBigEndianBytes does the grunt work, and this is passed to a BitConverter. There will be a definite problem if you read a large number of bytes since this will cause a large memory allocation.