Link to home
Start Free TrialLog in
Avatar of naseeam
naseeamFlag for United States of America

asked on

Macro to put data in Little Endian

My enivroment is Unix, C.  I get the following compile error on attached 'C' code.  Please advise.

"put_little_u32.c", line 8: error 4068: Missing comma seperator between par
ameters in macro "endian_put_native_to_little_u32".
#include <stdio.h>
 
/* This macro reverses data order so LSB is first. */
#define endian_put_native_to_little_u32(long src, char * dest) \
        dest[0] = (char)(src); \
	dest[1] = (char)(src >> 8); \
	dest[2] = (char)(src >> 16); \
	dest[3] = (char)(src >> 24);  /* line 8 */
 
 
main()     
{
   char data[4];  
   
   long pgn = 0x33221100;
   
   endian_put_native_to_little_u32(pgn, &data[0]);
   
   printf("data[0] = %d.\n", data[0]);
   printf("data[1] = %d.\n", data[1]);
   printf("data[2] = %d.\n", data[2]);
   printf("data[3] = %d.\n", data[3]);
}

Open in new window

Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

You just need to use as:
   endian_put_native_to_little_u32(pgn, data);
does it work?
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Maybe you are looking for htonl or ntohl ?

Regards
Friedrich
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also, if you pass &data[0] rather than data
&data[0][1] would be parsed as &(data[0][1])
and taking [0][1] of a char[4] makes no sense, so it has to be
(&data[0])[1]
Avatar of naseeam

ASKER

Expert Fixed my source code.  As a result my code compiled.