LSB优先还是MSB优先?

编程入门 行业动态 更新时间:2024-10-10 11:26:33
本文介绍了LSB优先还是MSB优先?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想用十六进制数转换一个十进制数,然后先用LSB通过串口发送。 我试过这个

I want to convert a decimal number in hexadecimal number and send through serial port with LSB first. I tried this

Int32 decValue = 1234056789; byte[] testBytes = new byte[4]; testBytes [0] = (byte)(decValue & 0x000000FF); testBytes [1] = (byte)((decValue >> 8) & 0x000000FF); testBytes [2] = (byte)((decValue >> 16) & 0x000000FF); testBytes [3] = (byte)((decValue >> 24) & 0x000000FF);

testBytes [0]是否包含LSB中的字节第一个或MSB优先???

Is testBytes[0] contains bytes in LSB first or MSB first???

推荐答案

严格来说,它取决于连接到RS-232电缆另一端的设备的要求。 Windows使用little-endian格式(最低有效字节优先);我会说,大多数系统都是这样做的。但是,这不是一个规则; big-endian也被使用。例如,所谓的标准网络订单是big-endian。 请参阅: en.wikipedia/wiki/Endianness [ ^ ]。
-SA
Strictly speaking, it depends on the requirements of the device connected to the other end of your RS-232 cable. Windows uses "little-endian" formats (least significant byte goes first); I would say, most systems do the same. However, this is not a rule; "big-endian" is also used. For example, so-called standard "network order" is big-endian. Please see: en.wikipedia/wiki/Endianness[^].
—SA

这是测试方法来自msdn: msdn.microsoft/en-us/library/bb384066 .aspx [ ^ ] This is the way to test taken from msdn: msdn.microsoft/en-us/library/bb384066.aspx[^] byte[] bytes = { 0, 0, 0, 25 }; int i = BitConverter.ToInt32(bytes, 0); Console.WriteLine("int: {0}", i);

如果这打印25,那么你的计算机是big-endian - 你首先发送testBytes [3]。 如果它不打印25然后发送testBytes [ 0]首先。 但是有一个对endian_ness BitConverter.IsLittleEndian的测试。所以你可以写:

If this prints 25 then your computer is big-endian - you send testBytes [3] first. If it does not print 25 then send testBytes [0] first. There is however a test for endian_ness BitConverter.IsLittleEndian. So you can write:

byte[] bytes = { 0, 0, 0, 25 }; // If the system architecture is little-endian (that is, little end first), // reverse the byte array. if (BitConverter.IsLittleEndian) Array.Reverse(bytes); int i = BitConverter.ToInt32(bytes, 0); Console.WriteLine("int: {0}", i); // Output: int: 25

您还应该查看BitConverter.GetBytes(Int32)以将Int32转换为字节数组。 msdn.microsoft/en-us/library/de8fssa4(v=vs.110).aspx [ ^ ]

更多推荐

LSB优先还是MSB优先?

本文发布于:2023-11-28 21:31:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644015.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:LSB   MSB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!