Click here to Skip to main content
Click here to Skip to main content

Structure Bit Ordering : Using ntoh and hton when transfering data between different platforms

By , 11 Mar 2002
 

Introduction

I have a 4-byte data, which is defined as:

Bits 31-24 Bits 21-23 Bit 20 Bit 19-10 Bit 9-0
Pixel Data Spare set to 0 Direction Pixel Y address (row) Pixel X address (col)

When using MS compiler on intel PC, its rule is: bit fields are laid out from the least significant bit to the most significant bit. So in my VC++ code, I define the structure as:

#pragma pack(1)
typedef struct
{
    unsigned int pixel_y_address : 10; // bits 0 - 9
    unsigned int pixel_x_address : 10; // bits 10 - 19
    unsigned int direction : 1; // bits 20
    unsigned int unused : 3; // bits 21-23
    unsigned int pixel_data : 8; // bits 24 - 31
} Image_Segment_Data;

Now I need to transfer these data to my program running on PowerPC. The operating system here is VxWorks. I found out that the rule here is reversed comparing with MS compiler: bit fields are laid out from the most significant bit to the least significant bit. So in my VxWorks code, I define the structure as:

struct image_segment_data_
{
    unsigned int pixel_data : 8; /* bits 24 - 31 */
    unsigned int unused : 3; /* bits 21-23 */
    unsigned int direction : 1; /* bits 20 */
    unsigned int pixel_x_address : 10; /* bits 10 - 19 */
    unsigned int pixel_y_address : 10; /* bits 0 - 9 */
} __attribute__ ((packed));

typedef image_segment_data_ Image_Segment_Data;

But the story does not stop here. When sending out data using TCP/IP socket, the MSB in PC code is received as LSB in VxWorks code. That is, data from PC {pppp,pppp,uuud,xxxx,xxxx,xxyy,yyyy,yyyy} is parsed by VxWorks as {yyyy,yyyy,xxxx,xxyy,uuud,xxxx,pppp,pppp}. What a hell! This is because the host byte order is not the same on these two platforms. So the data must be converted before sent by PC. To facilitate this, a union is defined as:

typedef union
{
    Image_Segment_Data data;
    unsigned long raw;
} Image_Segment_Union;
The reversing is easy:
Image_Segment_Data  originData;
Image_Segment_Union originUnion;
Image_Segment_Union tempUnion;

when sending from PC:
originUnion.data = originData;
tempUnion.raw = htonl(originUnion.raw) ;
send(sd, (char *)&tempUnion.raw, sizeof(tempUnion.raw), 0);

when receiving from PowerPC:
recv(sd, (char *)&tempUnion.raw, sizeof(tempUnion.raw));
originUnion.raw = ntohl(tempUnion.raw);
originData = originaUnion.data

Conclusion

That’s all I figured out. Thank you, Time Finer and axiac, for pointing out my mistake on understanding this issue.

Reference: Joaquín M López Muñoz’s answer to my question "Help! About structure alignment". (No wonder I love CP so much!)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

lucy
Software Developer
Canada Canada
Member
I love this place!

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
General??? when struct is bigger than sizeof(long)sussAnonymous11 Apr '03 - 8:07 
Generalnot a compiler issue...memberaxiac11 Mar '02 - 22:26 
GeneralRe: not a compiler issue...memberlucy12 Mar '02 - 5:14 
GeneralRe: not a compiler issue...sussAnonymous11 Apr '03 - 8:47 
GeneralRe: not a compiler issue...memberGameZelda11 Jan '10 - 11:27 
Generaltry using ntohl / htonlmemberTim Finer11 Mar '02 - 12:44 
For network byte ordering issues.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 12 Mar 2002
Article Copyright 2002 by lucy
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid