Byte Order in Struct

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Byte Order in Struct

Jump to solution
2,253 Views
rayhall
Contributor V

struct TestStruct {

   uint16_t itemOne;

   uint16_t itemTwo;

   uint16_t itemThree;

   uint16_t itemFour;

}test;

 

 

test.itemOne = 1000;

test.itemTwo = 1001;

test.itemThree = 1002;

test.itemFour = 1003;

 

 

The values are stored with LSB First. All other MCU I have used

and C++ Builder store the values MSB First. This has made simple

coding into more complicated as I have to swap the bytes before I

can use them when they are transferred to and from the PC software..

 

I am using the S12G128 if that matters.

 

How do I make CodeWarrior stop doing this.

 

Ray.

Labels (1)
0 Kudos
1 Solution
1,773 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

Hi,

yes this problem is due to big endian.

in addition, in Freesacle MCU,

Big Endian : HCS12(X), HC(S)08, ColdFire, …

Little Endian : DSC and Kinetis.

user can write convert code by themselves

The quickest way for converting 2-byte and 4-byte data  is to define macros:

/*applicable to both integer or floating point number which originally written for HC12*/

#define EndianConvert16(w) ((w>>8)|(w<<8))

#define EndianConvert32(dw) ((dw<<24)|((dw>>8)&0xFF00)|((dw<<8)&0xFF0000)|(dw>>24))

can this help?


Have a great day,
Zhang Jun

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

View solution in original post

0 Kudos
4 Replies
1,773 Views
rayhall
Contributor V

Maybe I have not explained clearly what the problem is.

test.testOne = 1000;

When test.itemOne value is read after being transferred to the PC by SCI, the value is 59395 not 1000.

Both the S12G128 and PC software have the same structure. The whole structure is transferred too a from S12 and PC via SCI.

I have been using this method for many years with the Atmel MCU and no problems. The S12 compiler is storing the 16 bit values in memory with the byte around the other way.

Ray.

0 Kudos
1,774 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

Hi,

yes this problem is due to big endian.

in addition, in Freesacle MCU,

Big Endian : HCS12(X), HC(S)08, ColdFire, …

Little Endian : DSC and Kinetis.

user can write convert code by themselves

The quickest way for converting 2-byte and 4-byte data  is to define macros:

/*applicable to both integer or floating point number which originally written for HC12*/

#define EndianConvert16(w) ((w>>8)|(w<<8))

#define EndianConvert32(dw) ((dw<<24)|((dw>>8)&0xFF00)|((dw<<8)&0xFF0000)|(dw>>24))

can this help?


Have a great day,
Zhang Jun

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
1,773 Views
rayhall
Contributor V

Thank you Edward and Zhang.

Ray.

0 Kudos
1,773 Views
kef2
Senior Contributor IV

1. S12 is big endian machine, which means >=16bit integers and floating point numbers are stored MSByte first. PC and Atmel MCU's are little endian machines, LSB first.

2. C structs are not portable. Even with the same byte order, one compiler may add more padding bytes to align struct members than another compiler may add.

Edward