C#的小端或大端? [英] C# little endian or big endian?

查看:873
本文介绍了C#的小端或大端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在硬件文档,使我们能够通过UDP / IP控制它,
我发现下面的代码片段:

In the documentation of hardware that allows us to control it via UDP/IP, I found the following fragment:

在此的通信协议,DWORD是一个4字节的数据,字是一个2​​个字节的数据,
  字节是单字节的数据。存储格式为小端,即4个字节(32位)的数据存储为:D7-D0,D15-D8,D23-D16,D31-D24;双字节(16位)的数据存储为:D7-D0,D15-D8

In this communication protocol, DWORD is a 4 bytes data, WORD is a 2 bytes data, BYTE is a single byte data. The storage format is little endian, namely 4 bytes (32bits) data is stored as: d7-d0, d15-d8, d23-d16, d31-d24; double bytes (16bits) data is stored as: d7-d0 , d15-d8.

我想知道如何转换为C#?
我有送过来之前的东西转换?
例如,如果我要送过来一个32位整数,或4字符串?

I am wondering how this translates to C#? Do I have to convert stuff before sending it over? For example, if I want to send over a 32 bit integer, or a 4 character string?

推荐答案

C#本身没有定义字节顺序。当你转换为字节,但是,你正在做的抉择。该 BitConverter 类有一个<一个href=\"http://msdn.microsoft.com/en-us/library/system.bitconverter.islittleendian.aspx\">IsLittleEndian现场告诉你这将如何表现,但它不给的选择。这同样适用于BinaryReader /的BinaryWriter。

C# itself doesn't define the endianness. Whenever you convert to bytes, however, you're making a choice. The BitConverter class has an IsLittleEndian field to tell you how it will behave, but it doesn't give the choice. The same goes for BinaryReader/BinaryWriter.

MiscUtil 图书馆有一个EndianBitConverter类,它允许您定义的字节顺序;也有类似等价BinaryReader /作家。没有在线使用指南我很害怕,但他们琐碎的:)

My MiscUtil library has an EndianBitConverter class which allows you to define the endianness; there are similar equivalents for BinaryReader/Writer. No online usage guide I'm afraid, but they're trivial :)

(EndianBitConverter还具有一项功能这是不正常的BitConverter,这是做转换就地字节数组present。)

(EndianBitConverter also has a piece of functionality which isn't present in the normal BitConverter, which is to do conversions in-place in a byte array.)

这篇关于C#的小端或大端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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