简体   繁体   English

是否有必要使用无符号8位整数检查字节顺序?

[英]Is it necessary to check for endianness with unsigned 8-bit integers?

I am sending data between a C TCP socket server and a C# TCP client. 我在C TCP套接字服务器和C#TCP客户端之间发送数据。

From the C# client, the data being sent is an array of the .NET framework type System.Byte, which is an unsigned 8-bit integer. 从C#客户端,发送的数据是.NET框架类型System.Byte的数组,它是一个无符号的8位整数。

From the C server, the data being sent is an array of the C type char, which is also an unsigned 8-bit integer. 从C服务器,发送的数据是C类型char的数组,它也是无符号的8位整数。

From what I have read, endianness is an issue when dealing with 16+ bit integers, ie when you have more than 1 byte, then the order of bytes is either in Little Endian and Big Endian. 从我所读到的,当处理16位整数时,字节顺序是一个问题,即当你有超过1个字节时,字节的顺序是Little Endian和Big Endian。

Since I am only transmitting 8-bit arrays, do I need to worry about endianness? 由于我只传输8位数组,我是否需要担心字节序? I haven't been able to find a clear answer so far. 到目前为止,我还没有找到一个明确的答案。

Thank you. 谢谢。

Your intuition is correct: endianness is irrelevant for 8-bit integers; 你的直觉是正确的:字节序与8位整数无关; it only comes into play for types that are wider than one byte. 它仅适用于宽度超过一个字节的类型。

The hardware takes care of the bit endianness, or more specifically bit endianness is defined in the physical layer. 硬件负责位字节序,或者更具体地说,位字节序在物理层中定义。 For instance if your packets are transmitted over ethernet they will be transmitted low-bit first. 例如,如果您的数据包通过以太网传输,它们将首先以低位传输。 But anyway after they have been received bytes will be reassembled the way you sent them by the physical layer. 但无论如何,在收到字节后,将按照物理层发送的方式重新组装字节。

Since you are dealing with a higher layer of the protocol-stack, you only have to care about byte endianness, which means you can not have problems with a 8 bits integer. 由于您正在处理协议栈的更高层,因此您只需关心字节字节序,这意味着您不会遇到8位整数的问题。

There are 2 kinds of endianness: 有两种字节顺序:

  • byte-endianness : the order of bytes within a multi-byte data byte-endianness :多字节数据中的字节顺序
  • bit-endianness : the order of bits within a byte bit-endianness :一个字节内的位顺序

Most of the time when you say endianness you say byte-endianness. 大多数时候,当你说endianness时,你会说byte-endianness。 That is because bit-endianness is almost always big-endian, but the byte-endianness varies across systems. 这是因为位端字符串几乎总是大端字节,但字节字节序在系统中会有所不同。

With byte-endianness you don't have to worry for data that is 1-byte width, as you suspect. 使用byte-endianness,您不必担心1字节宽度的数据,正如您所怀疑的那样。

Can't say how much role does bit-endianness play in the tcp protocol. 不能说bit-endianness在tcp协议中扮演了多少角色。 From the other answers here it looks like you don't have to worry about bit-endianness at the transport layer. 从其他答案来看,您似乎不必担心传输层的位端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM