Dear all,
I would like to handle a table of 4-byte long strings by just declaring
them as an array of unsigned 32 bit values.
Of course, this can be done manually, but it would be great if a macro
XY existed, that, for example, from
XY("ABCD") would generate a unsigned 32bit declaration like 0x41424344
Is anybody aware of a macro that does this?
Best regards,
Andre
OT: C macro to declare u32 from "ABCD"
Started by ●March 9, 2012
Reply by ●March 9, 20122012-03-09
Andre wrote:> Dear all, > > I would like to handle a table of 4-byte long strings by just declaring > them as an array of unsigned 32 bit values. > Of course, this can be done manually, but it would be great if a macro > XY existed, that, for example, from > XY("ABCD") would generate a unsigned 32bit declaration like 0x41424344 > > Is anybody aware of a macro that does this?How about XY('A', 'B', 'C', 'D') instead? For big endian CPUs you then want: #define XY(a,b,c,d) (((a) << 24) + ((b) << 16) + ((c) << 8) + (d)) and for little endian CPUs is should be: #define XY(a,b,c,d) ((a) + ((b) << 8) + ((c) << 16) + ((d) << 24)) Erik -- ---------------------------------------------------------------------- Erik de Castro Lopo http://www.mega-nerd.com/
Reply by ●March 9, 20122012-03-09
On Fri, 09 Mar 2012 11:25:36 +0100, Andre <lodwig@pathme.de> wrote:>I would like to handle a table of 4-byte long strings by just declaring >them as an array of unsigned 32 bit values. >Of course, this can be done manually, but it would be great if a macro >XY existed, that, for example, from >XY("ABCD") would generate a unsigned 32bit declaration like 0x41424344How about: union { char stringpart[4]; U32 wordpart; } XY; You will have big-endian/little-endian problems for arithmetic, but if you just want to move four bytes at a time, this should work. Greg
Reply by ●March 9, 20122012-03-09
On 3/9/12 7:30 AM, Greg Berchin wrote:> On Fri, 09 Mar 2012 11:25:36 +0100, Andre<lodwig@pathme.de> wrote: > >> I would like to handle a table of 4-byte long strings by just declaring >> them as an array of unsigned 32 bit values. >> Of course, this can be done manually, but it would be great if a macro >> XY existed, that, for example, from >> XY("ABCD") would generate a unsigned 32bit declaration like 0x41424344 > > How about: > > union { > char stringpart[4]; > U32 wordpart; > } XY; > > You will have big-endian/little-endian problems for arithmetic, but if you just > want to move four bytes at a time, this should work.there's the termination byte to worry about. maybe it should be union { char stringpart[5]; U32 wordpart; } XY; but i dunno how the u32 would line up, but i would assume that C would line it up at the left. also, i thought a lot of compilers used the single quote for literals that were longer than a char. char a_char = 'A'; short a_short = 'AB'; long a_long = 'ABCD'; dunno if this would work: long long a_long_long = 'ABCDEFGH'; -- r b-j rbj@audioimagination.com "Imagination is more important than knowledge."
Reply by ●March 9, 20122012-03-09
On Fri, 09 Mar 2012 11:38:32 -0500, robert bristow-johnson <rbj@audioimagination.com> wrote:>there's the termination byte to worry about. maybe it should be > >union { > char stringpart[5]; > U32 wordpart; >} XY; > >but i dunno how the u32 would line up, but i would assume that C would >line it up at the left.I got the impression that the OP meant that he had elements consisting of four characters, and not actual strings as C defines them (including a fifth termination element). I guess only he knows for certain. Greg
Reply by ●March 9, 20122012-03-09
Andre wrote:> I would like to handle a table of 4-byte long strings by just declaring > them as an array of unsigned 32 bit values. > Of course, this can be done manually, but it would be great if a macro > XY existed, that, for example, from > XY("ABCD") would generate a unsigned 32bit declaration like 0x41424344Decent compilers, especially for C++, optimize #define XY(s) (s[0]*16777216 + s[1]*65536 + s[2]*256 + s[3]) into a constant (that is, they would generate a 'load constant' assembler instruction for code like 'someVar = XY("ABCD")'), although this is not a constant expression as defined by the language standard and thus not usable as a switch label, for example. I generally use a macro like XY('A','B','C','D') instead. Stefan
Reply by ●March 9, 20122012-03-09
On Fri, 09 Mar 2012 06:30:03 -0600, Greg Berchin wrote:> On Fri, 09 Mar 2012 11:25:36 +0100, Andre <lodwig@pathme.de> wrote: > >>I would like to handle a table of 4-byte long strings by just declaring >>them as an array of unsigned 32 bit values. Of course, this can be done >>manually, but it would be great if a macro XY existed, that, for >>example, from >>XY("ABCD") would generate a unsigned 32bit declaration like 0x41424344 > > How about: > > union { > char stringpart[4]; > U32 wordpart; > } XY; > > You will have big-endian/little-endian problems for arithmetic, but if > you just want to move four bytes at a time, this should work. > > GregI'm not sure if this would work or not, and if it did I'm not sure if it would work in every compiler. As mentioned, in C when you type "ABCD", a section of memory gets set to 0x61, 0x62, 0x63, 0x64, 0x00. Moreover, in C when you declare char foo[] = "bar"; the string gets put where the compiler puts strings, which isn't necessarily where the compiler puts regular variables, then it declares the symbol 'foo' to point to the start of the string "bar". So I have little faith that XY foo = { "ABCD", "EFGH", "IJKL" }; will even compile, much less actually work. -- My liberal friends think I'm a conservative kook. My conservative friends think I'm a liberal kook. Why am I not happy that they have found common ground? Tim Wescott, Communications, Control, Circuits & Software http://www.wescottdesign.com
Reply by ●March 12, 20122012-03-12
Dear all,
I have found a solution like this in the meantime:
#define S2U32(s) (((u32)(*(s))) + 256*( ((u32)(*(s+1))) + 256*(
((u32)(*(s+2))) + 256*((u32)(*(s+3))) )))
with u32 being my (shorter!) typedef for unsigned 32 bits.
I am not really clear why this works, I had expected alignment errors,
but it does. A declaration like
u32 my_test[] =
{
S2U32("ABCD"),
S2U32("AAAA"),
S2U32("BLAB"),
S2U32("asdf"),
S2U32("abcd"),
S2U32("xyzz")
};
does what I want, it declares just an array of 32 bit unsigned values,
which I can then easily compare to an unsigned 32 bit that I an looking for.
Best regards,
Andre
On 09.03.2012 18:20, Greg Berchin wrote:
> On Fri, 09 Mar 2012 11:38:32 -0500, robert bristow-johnson
> <rbj@audioimagination.com> wrote:
>
>> there's the termination byte to worry about. maybe it should be
>>
>> union {
>> char stringpart[5];
>> U32 wordpart;
>> } XY;
>>
>> but i dunno how the u32 would line up, but i would assume that C would
>> line it up at the left.
>
> I got the impression that the OP meant that he had elements consisting of four
> characters, and not actual strings as C defines them (including a fifth
> termination element). I guess only he knows for certain.
>
> Greg
Reply by ●March 12, 20122012-03-12
Andre <lodwig@pathme.de> wrote:> I would like to handle a table of 4-byte long strings by just declaring > them as an array of unsigned 32 bit values. > Of course, this can be done manually, but it would be great if a macro > XY existed, that, for example, from > XY("ABCD") would generate a unsigned 32bit declaration like 0x41424344As far as I know, C doesn't require constant expression evaluation for operations on string constants. A macro such as that might be evaluated at run time, and can't, for example, be used to initialize a static variable. If instead YX('A','B','C','D') was good enough, then you should be able to reliably do it. Constants like 'A' are int constants (in C, not in C++) and are processed pretty much the same as contants like 65 or 193. I once tried to use a macro like you want for CASE constants, but instead had to use the YX form. -- glen
Reply by ●March 14, 20122012-03-14
Have you tried myLong = 'ABCD'; myFunc(arg1, 'ABCD', arg3...); etc.? Note single quotes.>Dear all, > >I would like to handle a table of 4-byte long strings by just declaring >them as an array of unsigned 32 bit values. >Of course, this can be done manually, but it would be great if a macro >XY existed, that, for example, from >XY("ABCD") would generate a unsigned 32bit declaration like 0x41424344 > >Is anybody aware of a macro that does this? > >Best regards, > >Andre >






