什么时候Endianness成为一个因素? [英] When does Endianness become a factor?

查看:104
本文介绍了什么时候Endianness成为一个因素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的理解,字节序是组成一个多字节字的字节顺序不同,至少在最典型的情况下。因此,16位整数可以存储为 0xHHLL 0xLLHH



假设我没有这个错误,我想知道的是,Endianness成为在两个计算机之间发送信息的主要因素,Endian可能或可能




  • 如果我传送一个短整数1,以char数组的形式,

  • 如果我使用下面的代码分解和重构短整数,那么字节序不再是一个因素吗?

      //发件人:
    for(n = 0,n< sizeof(uint16)* 8; ++ n)
    stl_bitset [n] =(value>> n)& 1;
    };

    //接收者:
    for(n = 0,n value | = uint16(stl_bitset [n] & 1)< n;
    };是否有标准的方式来补偿字节序?



提前感谢!

解决方案

很抽象地说,endianness是一个属性



实际上,这很重要的是当你 read()从一个字符数组重新解释一个变量。和 write()到外部字节流(像文件或套接字)。或者,再次抽象地说,当序列化数据时,序列重要性(实质上是因为序列化数据没有类型系统,只是由哑字节组成);并且字词序不会 在您的编程语言之下,因为该语言仅在而不是表示上运行。从一个到另一个是你需要挖掘的细节。



可以这样写:

  uint32_t n = get_number ; 

unsigned char bytesLE [4] = {n,n> 8,n> 16,n> 24}; // little-endian order
unsigned char bytesBE [4] = {n> 24,n> 16,n> 8,n}; // big-endian order

write(bytes ...,4);

这里我们可以说, reinterpret_cast< unsigned char *> & n),结果将取决于系统的字节顺序。



并阅读:

  unsigned char buf [4] = read_data(); 

uint32_t n_LE = buf [0] + buf [1]< 8 + buf [2] 16 + buf [3]< 24; // little-endian
uint32_t n_BE = buf [3] + buf [2]< 8 + buf [1]< 16 + buf [0] 24; // big-endian

同样,这里我们可以说, uint32_t n = * reinterpret_cast< uint32_t *>(buf),结果将取决于机器字节顺序。



p>

正如你所看到的,对于整数类型,如果使用代数输入和输出操作,你就不必知道自己的系统的字节序,只有数据流。对于其他数据类型,例如 double ,问题就更复杂了。


Endianness from what I understand, is when the bytes that compose a multibyte word differ in their order, at least in the most typical case. So that an 16-bit integer may be stored as either 0xHHLL or 0xLLHH.

Assuming I don't have that wrong, what I would like to know is when does Endianness become a major factor when sending information between two computers where the Endian may or may not be different.

  • If I transmit a short integer of 1, in the form of a char array and with no correction, is it received and interpretted as 256?

  • If I decompose and recompose the short integer using the following code, will endianness no longer be a factor?

    // Sender:
    for(n=0, n < sizeof(uint16)*8; ++n) {
        stl_bitset[n] = (value >> n) & 1;
    };
    
    // Receiver:
    for(n=0, n < sizeof(uint16)*8; ++n) {
        value |= uint16(stl_bitset[n] & 1) << n;
    };
    

  • Is there a standard way of compensating for endianness?

Thanks in advance!

解决方案

Very abstractly speaking, endianness is a property of the reinterpretation of a variable as a char-array.

Practically, this matters precisely when you read() from and write() to an external byte stream (like a file or a socket). Or, speaking abstractly again, endianness matters when you serialize data (essentially because serialized data has no type system and just consists of dumb bytes); and endianness does not matter within your programming language, because the language only operates on values, not on representations. Going from one to the other is where you need to dig into the details.

To wit - writing:

uint32_t n = get_number();

unsigned char bytesLE[4] = { n, n >> 8, n >> 16, n >> 24 };  // little-endian order
unsigned char bytesBE[4] = { n >> 24, n >> 16, n >> 8, n };  // big-endian order

write(bytes..., 4);

Here we could just have said, reinterpret_cast<unsigned char *>(&n), and the result would have depended on the endianness of the system.

And reading:

unsigned char buf[4] = read_data();

uint32_t n_LE = buf[0] + buf[1] << 8 + buf[2] << 16 + buf[3] << 24; // little-endian
uint32_t n_BE = buf[3] + buf[2] << 8 + buf[1] << 16 + buf[0] << 24; // big-endian

Again, here we could have said, uint32_t n = *reinterpret_cast<uint32_t*>(buf), and the result would have depended on the machine endianness.


As you can see, with integral types you never have to know the endianness of your own system, only of the data stream, if you use algebraic input and output operations. With other data types such as double, the issue is more complicated.

这篇关于什么时候Endianness成为一个因素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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