简体   繁体   中英

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.

The local machine gets the message as a buffer, ie dataBuffer which is an array of 4 16-bit ints. This buffer data eventually be mapped to a MainType data somewhere in the program but this is not our concern. We need a function that swaps the bytes (change the endianness) using the swapData() method.

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)?

Constraints:

  • The dataBuffer needs to be global in the program,
  • Swapping needs to be taken care of in swapData() function,
  • The data will be filled in some other method such as 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. 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.

Byte-swapping W is advisable because Wh most likely falls on a byte boundary with 1 bit in each byte. For its bits to be adjacent in the local machine, the bytes in W should be swapped. 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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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