简体   繁体   English

在将数据缓冲区映射到最终数据结构之前,我们是否可以交换数据缓冲区的字节顺序,这是一个固定大小的数组?

[英]Can we swap the endianness of a data buffer, which is a fixed sized array, before mapping it to its eventual data structure?

Scenario: A remote machine (big endian) sends a message to a local machine (little endian) over RS422. 场景:远程机器(大端)通过RS422向本地机器(小端)发送消息。

The local machine gets the message as a buffer, ie dataBuffer which is an array of 4 16-bit ints. 本地机器将消息作为缓冲区获取,即dataBuffer ,它是一个包含4个16位整数的数组。 This buffer data eventually be mapped to a MainType data somewhere in the program but this is not our concern. 此缓冲区数据最终会映射到程序中的某个MainType数据,但这不是我们关注的问题。 We need a function that swaps the bytes (change the endianness) using the swapData() method. 我们需要一个使用swapData()方法交换字节(更改字节顺序)的swapData()

Question: Given the fact that MainType has exactly 4 data members each 16 bits AND dataBuffer is array of size 4 and each data is 16 bits, can we just swap the data in the buffer WITHOUT mapping it to MainType data structure (as below)? 问题:鉴于MainType正好有4数据成员,每个16 bits AND dataBuffer是大小为4的数组,每个数据是16位,我们可以只交换缓冲区中的数据而不将其映射到MainType数据结构(如下所示)?

Constraints: 约束:

  • The dataBuffer needs to be global in the program, dataBuffer需要在程序中是全局的,
  • Swapping needs to be taken care of in swapData() function, 交换需要在swapData()函数中处理,
  • The data will be filled in some other method such as useData() data将用其他方法填充,例如useData()

Here is the code: 这是代码:

... 

typedef unsigned short int USINT16;

typedef struct { 
    USINT16  a : 1; 
    USINT16  b : 1; 
    USINT16  c : 1;                         
    USINT16  d : 1;                              
    USINT16  e : 1;  
    USINT16  f : 1;  
    USINT16  g : 1; 
    USINT16  h : 2;                                
    USINT16  i : 3;
    USINT16  j : 4; 
} OtherType; // 16 bits

typedef struct {   
    USINT16   X;
    USINT16   Y;
    USINT16   Z;
    OtherType W;  
} MainType;

...

unsigned short dataBuffer[4]; // available in global scope

...

void swapData() {
    receiveData(&dataBuffer); // data buffer is filled

    int i;  
    for (i = 0; i < 4; i++) {
        dataBuffer[i] = __builtin_bswap16(dataBuffer);
    }
    // The data is little endian now ?
}

...

void useData() {
    MainType data; // map the swapped buffer to data

    // use the data etc.
    ....
}

If the remote machine behavior is frozen, you can investigate and determine what is the encoding of bit-fields on that platform and translate the buffer received on the local machine appropriately. 如果远程计算机行为被冻结,您可以调查并确定该平台上的位字段的编码是什么,并适当地转换在本地计算机上接收的缓冲区。

Byte swapping all 16 bit entries, including W , is a good initial step, you might have to change the struct definition for OtherType to fit the order in which it is defined by the compiler for the remote machine. 字节交换所有16位条目(包括W )是一个很好的初始步骤,您可能必须更改OtherTypestruct定义以适合远程计算机的编译器定义的顺序。 You can determine that by transmitting samples from the remote machine where only 1 field is set to all bits one and the others stay 0 and print the 16 bit value received. 您可以通过从远程计算机传输样本来确定,其中只有1个字段设置为所有位1,其他位置保持为0并打印接收到的16位值。

Byte-swapping W is advisable because Wh most likely falls on a byte boundary with 1 bit in each byte. 建议使用字节交换W ,因为Wh很可能落在字节边界上,每个字节有1位。 For its bits to be adjacent in the local machine, the bytes in W should be swapped. 要使其在本地机器中相邻的位,应交换W的字节。 If the bit order on the remote machine is abcdefg h1 h0 i2 i1 i0 j3 j2 j1 j0 for the whole 16 bit word in W , when stored in memory on the remote machine, it becomes <abcdefg h1> <h0 i2 i1 i0 j3 j2 j1 j0> and subsequently transmitted as bytes and loaded in a 16 bit register on the local machine, it would become h0 i2 i1 i0 j3 j2 j1 j0 abcdefg h1 if you dont swap the bytes, because the first byte is loaded in the low order bits of the register. 如果远程机器上的位顺序是对于W的整个16位字abcdefg h1 h0 i2 i1 i0 j3 j2 j1 j0 ,当存储在远程机器的存储器中时,它变为<abcdefg h1> <h0 i2 i1 i0 j3 j2 j1 j0>随后作为字节发送并加载到本地机器上的16位寄存器中,如果交换字节,它将变为h0 i2 i1 i0 j3 j2 j1 j0 abcdefg h1 ,因为第一个字节以低位加载寄存器的位。 Byte swapping prevents this but you still may have a problem with the bit-field order in the local machine as your current definition might be encoded as j3 j2 j1 j0 i2 i1 i0 h1 h0 gfedcba if the bit-fields are likely allocated from the lowest to the highest bit positions. 字节交换可以防止这种情况,但是您仍然可能在本地计算机中遇到位字段顺序问题, j3 j2 j1 j0 i2 i1 i0 h1 h0 gfedcba如果位字段可能从最低位分配,则当前定义可能被编码为j3 j2 j1 j0 i2 i1 i0 h1 h0 gfedcba到最高位位置。

If you know some assembly language, generate the assembly for code that manipulate the bit-fields on both platforms and check if the fields are placed differently. 如果您知道某种汇编语言,请生成用于操作两个平台上的位字段的代码的程序集,并检查字段的放置方式是否不同。

暂无
暂无

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

相关问题 当服务器以c结构发送数据时,我们可以在客户端上将缓冲区转换为C ++结构吗? - Can we typecast buffer into C++ structure on client when server is sending data as c structure? 我们可以将位字段用于c中的结构成员的数组吗? - Can we use bit field for an array which is member of the structure in c? 适当的数据包缓冲区数据结构 - Appropriate data structure for a buffer of the packets 使用哪种数据结构来模拟一个数组,在该数组中可以在任何位置添加数据? - What data structure to use to emulate an array in which one can add data in any position? 我们如何创建一个结构数组,它将灵活数组作为最后一个字段? - How do we create an array of structure, which has the flexible array as its last field? 如何通过作为结构成员的指针访问数组缓冲区? - How can i access a array buffer through a pointer which is a member of a structure? 如何在缓冲区/数组中缓存部分数据并将其他所有内容存储在 C 中的数据结构成员中 - How to cache part of the data in buffer/ array and have everything else stored in members of data structure in C 最小值的数据结构及其在变异数组中的偏移量 - Data structure for the minimum value and its offset in a mutating array 转换结构数据的字节顺序 - Converting endianness of struct-Data 结构标签可以在其作用域之前使用吗? - Can a structure tag be used before its scope?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM