<legend id='9vWY0'><style id='9vWY0'><dir id='9vWY0'><q id='9vWY0'></q></dir></style></legend>
  1. <small id='9vWY0'></small><noframes id='9vWY0'>

    <tfoot id='9vWY0'></tfoot>

      <bdo id='9vWY0'></bdo><ul id='9vWY0'></ul>

      <i id='9vWY0'><tr id='9vWY0'><dt id='9vWY0'><q id='9vWY0'><span id='9vWY0'><b id='9vWY0'><form id='9vWY0'><ins id='9vWY0'></ins><ul id='9vWY0'></ul><sub id='9vWY0'></sub></form><legend id='9vWY0'></legend><bdo id='9vWY0'><pre id='9vWY0'><center id='9vWY0'></center></pre></bdo></b><th id='9vWY0'></th></span></q></dt></tr></i><div id='9vWY0'><tfoot id='9vWY0'></tfoot><dl id='9vWY0'><fieldset id='9vWY0'></fieldset></dl></div>
    1. 字节序何时成为一个因素?

      时间:2023-09-17
        <tbody id='0a6xs'></tbody>

            • <bdo id='0a6xs'></bdo><ul id='0a6xs'></ul>

            • <legend id='0a6xs'><style id='0a6xs'><dir id='0a6xs'><q id='0a6xs'></q></dir></style></legend>
            • <tfoot id='0a6xs'></tfoot>
              • <small id='0a6xs'></small><noframes id='0a6xs'>

                <i id='0a6xs'><tr id='0a6xs'><dt id='0a6xs'><q id='0a6xs'><span id='0a6xs'><b id='0a6xs'><form id='0a6xs'><ins id='0a6xs'></ins><ul id='0a6xs'></ul><sub id='0a6xs'></sub></form><legend id='0a6xs'></legend><bdo id='0a6xs'><pre id='0a6xs'><center id='0a6xs'></center></pre></bdo></b><th id='0a6xs'></th></span></q></dt></tr></i><div id='0a6xs'><tfoot id='0a6xs'></tfoot><dl id='0a6xs'><fieldset id='0a6xs'></fieldset></dl></div>

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

                问题描述

                据我所知,字节序是指组成多字节字的字节顺序不同,至少在最典型的情况下是这样.这样一个 16 位整数可以存储为 0xHHLL0xLLHH.

                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.

                假设我没有错,我想知道的是,在 Endian 可能不同也可能不同的两台计算机之间发送信息时,Endianness 何时成为主要因素.

                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.

                • 如果我以 char 数组的形式传输一个短整数 1,并且没有更正,它是否被接收并解释为 256?

                • 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;
                };
                

              • 是否有一种标准的方式来补偿字节序?
              • 提前致谢!

                推荐答案

                非常抽象地说,字节序是将变量重新解释为字符数组的属性.

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

                实际上,当您从外部字节流(如文件或套接字)read()write() 到外部字节流时,这很重要.或者,再次抽象地说,当您序列化数据时,字节序很重要(主要是因为序列化数据没有类型系统,仅由哑字节组成);和字节序在你的编程语言中重要,因为该语言只对进行操作,而不对表示进行操作.从一个到另一个是你需要深入研究细节的地方.

                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.

                机智 - 写作:

                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(&n),结果将取决于系统的字节序.

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

                阅读:

                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(buf),结果将取决于机器字节序.

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


                如您所见,如果您使用代数输入和输出操作,对于整数类型,您永远不必知道自己系统的字节序,只需要知道数据流的字节序.对于其他数据类型,例如 double,问题会更加复杂.

                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.

                这篇关于字节序何时成为一个因素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:C++/Boost:示例中的未定义符号? 下一篇:如何获取 boost::asio::ip::tcp::socket 的 IP 地址?

                相关文章

                最新文章

                <tfoot id='sd86S'></tfoot>

                1. <legend id='sd86S'><style id='sd86S'><dir id='sd86S'><q id='sd86S'></q></dir></style></legend>
                    <bdo id='sd86S'></bdo><ul id='sd86S'></ul>

                  <small id='sd86S'></small><noframes id='sd86S'>

                    <i id='sd86S'><tr id='sd86S'><dt id='sd86S'><q id='sd86S'><span id='sd86S'><b id='sd86S'><form id='sd86S'><ins id='sd86S'></ins><ul id='sd86S'></ul><sub id='sd86S'></sub></form><legend id='sd86S'></legend><bdo id='sd86S'><pre id='sd86S'><center id='sd86S'></center></pre></bdo></b><th id='sd86S'></th></span></q></dt></tr></i><div id='sd86S'><tfoot id='sd86S'></tfoot><dl id='sd86S'><fieldset id='sd86S'></fieldset></dl></div>