简体   繁体   English

大尾小问题

[英]Big and Little endian question

I have the following code: 我有以下代码:

    // Incrementer
    datastores.cmtDatastores.u32Region[0] += 1;

    // Decrementer
    datastores.cmtDatastores.u32Region[1] = (datastores.cmtDatastores.u32Region[1] == 0) ? 
        10 : datastores.cmtDatastores.u32Region[1] - 1;

    // Toggler
    datastores.cmtDatastores.u32Region[2] = 
        (datastores.cmtDatastores.u32Region[2] == 0x0000) ? 
        0xFFFF : 0x0000;

The u32Region array is an unsigned int array that is part of a struct. u32Region数组是一个无符号的int数组,它是结构的一部分。 Later in the code I convert this array to Big endian format: 稍后在代码中,我将此数组转换为Big endian格式:

unsigned long  *swapL = (unsigned long*)&datastores.cmtDatastores.u32Region[50];
for (int i=0;i<50;i++) 
{
   swapL[i] = _byteswap_ulong(swapL[i]);
}

This entire code snippet is part of a loop that repeats indefinitely. 整个代码段是无限期重复的循环的一部分。 It is a contrived program that increments one element, decrements another and toggles a third element. 这是一个人为设计的程序,它增加一个元素,减少另一个元素并切换第三个元素。 The array is then sent via TCP to another machine that unpacks this data. 然后将该阵列通过TCP发送到另一台解压缩该数据的机器。

The first loop works fine. 第一个循环工作正常。 After that, since the data is in big endian format, when I "increment", "decrement", and "toggle", the values are incorrect. 之后,由于数据为大端格式,因此当我“递增”,“递减”和“切换”时,值不正确。 Obviously, if in the first loop datastores.cmtDatastores.u32Region[0] += 1; 显然,如果在第一个循环中, datastores.cmtDatastores.u32Region[0] += 1; results in 1, the second loop it should be 2, but it's not. 结果为1,第二个循环应该为2,但不是。 It is adding the number 1(little endian) to the number in datastores.cmtDatastores.u32Region[0] (big endian). 它将数字1(小尾数)加到datastores.cmtDatastores.u32Region[0] (大尾数)中的数字上。

I guess I have to revert back to little endian at the start of every loop, but it appears there should be an easier way to do this. 我想我必须在每个循环的开始时恢复为小端字节序,但是似乎应该有一种更简单的方法来执行此操作。

Any thoughts? 有什么想法吗?

Thanks, 谢谢,

Bobby 鲍比

The way I think about endian issues, is that there are numbers (when they are in the machine's endian order) and there are blobs of binary data (when they are not in the machines endian order). 我对字节序问题的思考方式是:有数字(当它们按机器的字节序排列时)和二进制数据的斑点(当它们不按机器的字节序顺序排列时)。

When you think like this, you realize you can't increment a blob of binary data (or do any numeric operations on them). 当您这样想时,您意识到您无法递增二进制数据的blob(或对其进行任何数字运算)。 The only thing you can do with them is write the raw data or convert them to a number. 您只能使用它们写原始数据或将它们转换为数字。

If you want to do numeric operations, the data must be a number so must be in the machine's endian order. 如果要执行数字运算,则数据必须是数字,因此必须以机器的字节序排列。

If the data always has to be bigendian to be sent out over TCP, then perhaps it would be simpler to leave the data in the array as always in bigendian order, and do the byte swap when you perform operations on the data. 如果数据始终必须是bigendian才能通过TCP发送出去,那么将数据按bigendian顺序始终保留在数组中,并在对数据执行操作时进行字节交换可能会更简单。 The increment would be come read from array, byteswap (to littleEndian), increment, byteswap (bigEndian), store to array. 增量将从数组,byteswap(到littleEndian),增量,byteswap(bigEndian),存储到数组中读取。

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

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