Converting command and query structure byte order

When the CG board receives data, it expects the byte ordering in little endian format, where bytes with the higher address (those furthest to the right) are most significant. However, certain operating systems (such as SPARC Solaris) use big endian byte ordering and consider the lower address bytes (bytes furthest to the left) most significant.

Use the following macros to convert values from host byte ordering to board byte ordering format for operating systems that use big endian byte order:

H2NMS_DWORD(DWORD val) 
H2NMS_WORD(WORD val) 

To convert values from CG board byte ordering to big endian byte ordering format, use the following macros:

NMS2H_DWORD(DWORD val)
NMS2H_WORD(WORD val)

 

Using these macros does not harm systems that use little endian byte ordering. Because of this, we recommend that you use them whenever dealing with command or query structures.

For host systems that use big endian byte ordering, applications must convert the following parameter structures from big endian to little endian (or vice versa) format:

Not all fields within these structures are effected. Only unsigned short (defined as WORD in nmstypes.h) and unsigned long (defined as DWORD in nmstypes.h) data types are effected.

Example

The following example code shows how to use the byte order conversion macros:

DWORD               dword;
WORD                word; 

/* From host to board byte ordering format */
   dword  = H2NMS_DWORD(dword);
   word = H2NMS_WORD(word);

// From board to host byte ordering format */
   dword = NMS2H_DWORD(dword);
   word = NMS2H_WORD(word);

 

These macros do not need to be wrapped around an #ifdef _OS_ preprocessor. On systems that do not need any conversion, output from these macros is the same as input.