如何才能简化网络字节顺序从BinaryReader在一个转换? [英] How can one simplify network byte-order conversion from a BinaryReader?

查看:563
本文介绍了如何才能简化网络字节顺序从BinaryReader在一个转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

这篇关于如何才能简化网络字节顺序从BinaryReader在一个转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆