C
Chen Shu
Hi there:
These days I came to a problem that I failed to convert a binary
number to a hex number using a macro. For example:
int a = BINARY4(0101) will become int a = 0x5;
int a = BINARY8(0001,0101) will become int a = 0x15;
...etc.
I've written the following macros:
#define HEX_0000 0
#define HEX_0101 5
... ...
#define HEX_(a) HEX_ ## a
#define COMBINE(a) (0x ## a)
#define BINARY(a) COMBINE(HEX_(a))
These macros work all right in VC7, but when I compile the program
using VC8 or GCC-3.4.4, the result is wrong. When using GCC, the result
is:
int a = BINARY4(0101) --> int a = (0xHEX_(0101)); // COMBINE() is
explained first;
It seems that the order of macro explanation is different in VC7
and GCC.
Well, which one is right? Does standard C support recursive macro
explanation (just like what VC7 does)?
If I want to write such a convert macro which can be compiled by
VC7 and GCC, how should I do?
Thanks a lot!
These days I came to a problem that I failed to convert a binary
number to a hex number using a macro. For example:
int a = BINARY4(0101) will become int a = 0x5;
int a = BINARY8(0001,0101) will become int a = 0x15;
...etc.
I've written the following macros:
#define HEX_0000 0
#define HEX_0101 5
... ...
#define HEX_(a) HEX_ ## a
#define COMBINE(a) (0x ## a)
#define BINARY(a) COMBINE(HEX_(a))
These macros work all right in VC7, but when I compile the program
using VC8 or GCC-3.4.4, the result is wrong. When using GCC, the result
is:
int a = BINARY4(0101) --> int a = (0xHEX_(0101)); // COMBINE() is
explained first;
It seems that the order of macro explanation is different in VC7
and GCC.
Well, which one is right? Does standard C support recursive macro
explanation (just like what VC7 does)?
If I want to write such a convert macro which can be compiled by
VC7 and GCC, how should I do?
Thanks a lot!