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
 
<!-- Add the rest of your HTML here -->

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

 
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 
How would I convert a structure that is bigger than a long to network byte order? Your example works fine for structures that are 4 bytes or smaller but what if I want to send a structure such as
 
struct {
   long timestamp;    // 4 bytes
   int mouseX;        // 2 bytes
   int mouseY;        // 2 bytes
   char key;          // 1 byte
   int alt:    1;     // 1 byte total for the rest
   int ctrl:   1;     
   int shift:  1;
   int unused: 5;
};

Generalnot a compiler issue...memberaxiac11-Mar-02 22:26 
The byte order is platform specific not compiler specific. I think any compiler would generate similar code on a given platform. Use htonl() whne send the data through TCP/IP and ntohl() on receive, before using it.

 
--
axiac
GeneralRe: not a compiler issue...memberlucy12-Mar-02 5:14 
I tried it out. it's exactly as what you and Tim said. I am sorry about my mistake.
GeneralRe: not a compiler issue...sussAnonymous11-Apr-03 8:47 
The reason why hton and ntoh works is that they check to see if the platform is little-endian or big-endian. See code below. I am not sure if network byte order is either little-endian or big-endian but if the endian-ness is not correct, hton and ntoh convert the value to network byte order and back again respectivly.
 

// Not sure if this is correct, since I did not compile and run it.

inline bool isBigEndian(){
   unsigned int num = x0002; // is the number 2 on a big-endian platform

   return (num == 2);
}

GeneralRe: not a compiler issue...memberGameZelda11-Jan-10 11:27 
Code's not valid, will return the same on all platforms, using the hex specifier has nothing to do with endianess.
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
Web02 | 2.6.130617.1 | Last Updated 12 Mar 2002
Article Copyright 2002 by lucy
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid