Solved

Macro to put data in Little Endian

Posted on 2007-11-30
7
336 Views
Last Modified: 2010-04-21
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

0
Comment
Question by:naseeam
7 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20386568
You just need to use as:
   endian_put_native_to_little_u32(pgn, data);
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20386779
does it work?
0
 
LVL 84

Accepted Solution

by:
ozo earned 360 total points
ID: 20387262
#include <stdio.h>

/* This macro reverses data order so LSB is first. */
#define endian_put_native_to_little_u32(src, 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]);
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 24

Expert Comment

by:fridom
ID: 20387307
Maybe you are looking for htonl or ntohl ?

Regards
Friedrich
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 140 total points
ID: 20390883
>> "put_little_u32.c", line 8: error 4068: Missing comma seperator between parameters in macro "endian_put_native_to_little_u32".

macro's do not take arguments like functions do ... Macro arguments are typeless, so you don't have to specify the types of the arguments. They just get treated as a string, and are literally replaced in the macro body.

See ozo's corrections.

Also, take a look at fridom's suggestion ! ;)
0
 
LVL 84

Expert Comment

by:ozo
ID: 20391805
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]
0
 
LVL 1

Author Closing Comment

by:naseeam
ID: 31412065
Expert Fixed my source code.  As a result my code compiled.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to get time_t value that is a week old 6 169
Passing a array as parameter - C 2 103
How to install SVN Command Line Client? 5 99
Secondary drive is failing... D Volume 11 48
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question