473,287 Members | 1,643 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

declaring long hex strings?

Hello, I have the following problem:

when declaring hex data in C, you typically do something like:
const char[] = {0xde, 0xad, 0xbe, 0xef, 0x01, 0x02, 0x03, 0x04 ... };

This is quite verbose and makes the source hard to read.

An alternative is something like
const char[] = "deadbeef01020304";

but this doubles the storage requirements since each nybble is
converted to an ASCII char.

Some old assemblers have HEX directives for declaring large chunks of
raw data. My question is: is there any way to do this in C? Excessive
preprocessor acrobatics are OK as long as they work in gcc...

The hex data I have is several thousand strings like:
CRC(b746de6d) SHA1(ea69f87f84ded1f0a66457af24cbc692e5ff67e3)

Really, I just want a preprocessor macro that does a trivial regex like
s/([a-fA-F0-9][a-fA-F0-9])/0x\1,/ is that possible?

Nov 14 '05 #1
11 4320
ar******@mac.com wrote:
Hello, I have the following problem:

when declaring hex data in C, you typically do something like:
const char[] = {0xde, 0xad, 0xbe, 0xef, 0x01, 0x02, 0x03, 0x04 ... };
Better
const unsigned char[] = ....
0x** is an unsigned int value and the conversion to char can cause
an overflow if char is signed which leads to undefined behaviour.
This is quite verbose and makes the source hard to read.
Indeed.

An alternative is something like
const char[] = "deadbeef01020304";

but this doubles the storage requirements since each nybble is
converted to an ASCII char.
Nope. C does not specify anything about character encoding;
we can as well use EBCDIC or anything else. The 4bit nibbles
are represented by a character which has CHAR_BIT bits.

Some old assemblers have HEX directives for declaring large chunks of
raw data. My question is: is there any way to do this in C? Excessive
preprocessor acrobatics are OK as long as they work in gcc...

The hex data I have is several thousand strings like:
CRC(b746de6d) SHA1(ea69f87f84ded1f0a66457af24cbc692e5ff67e3)

Really, I just want a preprocessor macro that does a trivial regex like
s/([a-fA-F0-9][a-fA-F0-9])/0x\1,/ is that possible?


I fear you won't get that; the preprocessor cannot split preprocessing
tokens.

Some possibilities:
If you have file I/O on your target system: Store your data chunks
in text or binary files and retrieve them at runtime. May even make
the whole thing more flexible.
Have a look at a general purpose preprocessor or a script language
with regexps and produce data files which will get converted to
the required C data which in turn can be #included where appropriate.
Third: If you are daring, you can fake a preprocesser macro; you "call"
e.g. const unsigned char[] = HEXARRAY(deadbeef....);
and apply, say, a perl script to your source before you go through
preprocessing and compilation; this is a viable solution if you use
makefiles.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #2
ar******@mac.com wrote:
...
when declaring hex data in C, you typically do something like:
const char[] = {0xde, 0xad, 0xbe, 0xef, 0x01, 0x02, 0x03, 0x04 ... };

This is quite verbose and makes the source hard to read.

An alternative is something like
const char[] = "deadbeef01020304";

but this doubles the storage requirements since each nybble is
converted to an ASCII char.


You can also use

const char[] = "\xde\xad\xbe\xef\x01\x02\x03\x04";

although this is not much less verbose than the original version.

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #3
Michael Mair wrote:
0x** is an unsigned int value and the conversion to char can cause
an overflow if char is signed which leads to undefined behaviour.


0x01 is an expression of type int, not unsigned.

Conversion of an out of range value, to a signed integer type,
is implementation defined, not undefined.

--
pete
Nov 14 '05 #4
pete wrote:
Michael Mair wrote:
0x** is an unsigned int value and the conversion to char can cause
an overflow if char is signed which leads to undefined behaviour.


0x01 is an expression of type int, not unsigned.

Conversion of an out of range value, to a signed integer type,
is implementation defined, not undefined.


Argh. Note to self: Do not put the standard into some unmarked
box when moving...
Right, I don't know what I was thinking (too late for thinking,
probably). Thank you :-)
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #5
Thanks for the replies.

It looks like there is no way to trick the preprocessor to do this. :(
Sadly in my case external files and additional preprocess passes are
not possible.

That leaves: ugly code. :(

Nov 14 '05 #6
ar******@mac.com wrote:
Hello, I have the following problem:

when declaring hex data in C, you typically do something like:
const char[] = {0xde, 0xad, 0xbe, 0xef, 0x01, 0x02, 0x03, 0x04 ... };

This is quite verbose and makes the source hard to read.

An alternative is something like
const char[] = "deadbeef01020304";

but this doubles the storage requirements since each nybble is
converted to an ASCII char.

Some old assemblers have HEX directives for declaring large chunks of
raw data. My question is: is there any way to do this in C? Excessive
preprocessor acrobatics are OK as long as they work in gcc...

The hex data I have is several thousand strings like:
CRC(b746de6d) SHA1(ea69f87f84ded1f0a66457af24cbc692e5ff67e3)

Really, I just want a preprocessor macro that does a trivial regex like s/([a-fA-F0-9][a-fA-F0-9])/0x\1,/ is that possible?


If you're willing to split it up, you can do something like...

#define SHA1(p,q,r,s,t) \
SPLIT(p), SPLIT(q), SPLIT(r), SPLIT(s), SPLIT(t)

#define CAT(x,y) x ##y
#define CAT2(x,y) CAT(x,y)

#define SPLIT(x) SPLIT4(CAT2(0x0,x))
#define SPLIT4(x) \
(x >> 24) & 0xFF, \
(x >> 16) & 0xFF, \
(x >> 8) & 0xFF, \
(x ) & 0xFF

unsigned char x[] =
{ SHA1(ea69f87f, 84ded1f0, a66457af, 24cbc692, e5ff67e3),
SHA1(deadbeef, aaaaaaaa, 55555555, 12345678, 90abcdef) };

#include <stdio.h>

int main(void)
{
size_t i;
for (i = 0; i < sizeof x; i++)
printf(" %02X", 0u + x[i]);
puts("");
return 0;
}

--
Peter

Nov 14 '05 #7
Michael Mair wrote:

pete wrote:
Michael Mair wrote:
0x** is an unsigned int value and the conversion to char can cause
an overflow if char is signed which leads to undefined behaviour.


0x01 is an expression of type int, not unsigned.

Conversion of an out of range value, to a signed integer type,
is implementation defined, not undefined.


Argh. Note to self: Do not put the standard into some unmarked
box when moving...
Right, I don't know what I was thinking (too late for thinking,
probably). Thank you :-)


You're welcome. I just thought of a C riddle.
#define A, B, and C, such that this expression
(A > B && B > C && A == C)
becomes a constant expression with a value of 1.

--
pete
Nov 14 '05 #8
pete <pf******@mindspring.com> writes:
I just thought of a C riddle.
#define A, B, and C, such that this expression
(A > B && B > C && A == C)
becomes a constant expression with a value of 1.


#define A 1 || (0
#define B 0
#define C 0)

The 1 may be replaced by an arbitrary nonzero value of your choice.
Each 0 may be replaced by an arbitrary value of your choice.
--
"Large amounts of money tend to quench any scruples I might be having."
-- Stephan Wilms
Nov 14 '05 #9
pete wrote:
Michael Mair wrote:
pete wrote:
Michael Mair wrote:
0x** is an unsigned int value and the conversion to char can cause
an overflow if char is signed which leads to undefined behaviour.

0x01 is an expression of type int, not unsigned.

Conversion of an out of range value, to a signed integer type,
is implementation defined, not undefined.


Argh. Note to self: Do not put the standard into some unmarked
box when moving...
Right, I don't know what I was thinking (too late for thinking,
probably). Thank you :-)

You're welcome. I just thought of a C riddle.
#define A, B, and C, such that this expression
(A > B && B > C && A == C)
becomes a constant expression with a value of 1.


*g*

#include <stdio.h>

#define STRINGIZE(s) #s
#define STRINGIZE2(s,t) "\""#s ", "#t"\""
#define XSTR(s) STRINGIZE(s)
#define XSTR2(s) STRINGIZE2(s)
#define EVAL \
printf("Evaluates (%s,%s,%s) to %d\n",\
XSTR(A),XSTR(B),XSTR(C),(A > B && B > C && A == C))
#define EVAL2 \
printf("Evaluates (%s,%s,%s) to %d\n",\
XSTR(A),XSTR(B),XSTR2(C),(A > B && B > C && A == C))

int main (void)
{
#define A 1||-1000
#define B -500
#define C 0
EVAL;
#undef A
#undef B
#undef C
#define A -1000
#define B -500
#define C 0, 1
EVAL2;
#undef A
#undef B
#undef C
#define A -1000
#define B -500
#define C 0 ? 0 : 1
EVAL;
#undef A
#undef B
#undef C
#define A (unsigned) C
#define B 0
#define C -1
EVAL;
#undef A
#undef B
#undef C

return 0;
}
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #10
Ben Pfaff wrote:

pete <pf******@mindspring.com> writes:
I just thought of a C riddle.
#define A, B, and C, such that this expression
(A > B && B > C && A == C)
becomes a constant expression with a value of 1.


#define A 1 || (0
#define B 0
#define C 0)

The 1 may be replaced by an arbitrary nonzero value of your choice.
Each 0 may be replaced by an arbitrary value of your choice.


I should have used more parentheses.
((A) > (B) && (B) > (C) && (A) == (C))

This is what I had in mind:
#define A (0)
#define B (-1)
#define C (0u)

--
pete
Nov 14 '05 #11
Michael Mair wrote:

pete wrote:
Michael Mair wrote:
pete wrote:

Michael Mair wrote:
>0x** is an unsigned int value and the conversion to char can cause
>an overflow if char is signed which leads to undefined behaviour.

0x01 is an expression of type int, not unsigned.

Conversion of an out of range value, to a signed integer type,
is implementation defined, not undefined.

Argh. Note to self: Do not put the standard into some unmarked
box when moving...
Right, I don't know what I was thinking (too late for thinking,
probably). Thank you :-)

You're welcome. I just thought of a C riddle.
#define A, B, and C, such that this expression
(A > B && B > C && A == C)
becomes a constant expression with a value of 1.

#define A 1||-1000
#define B -500
#define C 0
EVAL;
#undef A
#undef B
#undef C
#define A -1000
#define B -500
#define C 0, 1
EVAL2;
#undef A
#undef B
#undef C
#define A -1000
#define B -500
#define C 0 ? 0 : 1
EVAL;
#undef A
#undef B
#undef C
#define A (unsigned) C
#define B 0
#define C -1


That last set was closest to what I was thinking.
Thanks.
Nov 14 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Stefanie | last post by:
Hi, I have a question about declaring variables for writing data to a file for random access. I have two string variables declared in a module (user-defined types). The point is that for one of...
83
by: kartik | last post by:
there seems to be a serious problem with allowing numbers to grow in a nearly unbounded manner, as int/long unification does: it hides bugs. most of the time, i expect my numbers to be small. 2**31...
3
by: darrel | last post by:
Often, I want to use a string in multiple functions on a page. I can declare and re-set the string in each function, or I can just delcare the string outside of the individual functions so that...
4
by: snow.carriers | last post by:
http://www.rafb.net/paste/results/fTQgRW16.html Here's my program so far. This is what I'm trying to do: http://contest-cemc.uwaterloo.ca/ccc/2005/senior/phone.pdf So far it works fine. The only...
3
by: ajaksu | last post by:
Hello c.l.p.ers :) Running long(Decimal) is pretty slow, and the conversion is based on strings. I'm trying to figure out whether there is a good reason for using strings like in decimal.py...
15
by: edson | last post by:
Greetings. My microcontroller program uses arrays of strings. I want to store the strings AND their pointers in ROM. I am able to initialize the arrays so that the strings are put in ROM, but the...
2
by: Jean-Paul Calderone | last post by:
On Fri, 5 Sep 2008 14:24:16 -0500, Robert Dailey <rcdailey@gmail.comwrote: mystring = ( "This is a very long string that " "spans multiple lines and does " "not include line breaks or tabs "...
8
by: bintom | last post by:
What are the differences between the following methods of declaring strings? char string1 = "C++ forum"; char* string2 = "C++ forum"; I know that the first uses the array notation, whereas...
1
by: JLORA3659 | last post by:
dsPlcTagDataSet = new DataSet(); dsPlcTagDataSet.ReadXml(Application.StartupPath + "\\" + "PlcTag.xml"); in the xml file there is a row "iWord" that I need to sort later on but this row is...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.