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

查看:32
本文介绍了如何简化 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.

推荐答案

没有内置转换器.这是我的包装器(如您所见,我只实现了我需要的功能,但结构很容易根据您的喜好进行更改):

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天全站免登陆