C-Preprocessor Experts wanted...

  • Thread starter Klaus Siegesleitner
  • Start date
K

Klaus Siegesleitner

Hi all!
sorry, my first posting to this newsgroup - hope everything works fine...

I'm looking for a macro that converts an octal number assigned to a
preprocessor symbol (via a simple #define) into a string-fragment that can
be added/embedded to an new string literal.
This string-fragment shall consist of two characters, both defined as octal
numbers:
the first octal number \001 (acting as an escape symbol) and
the second octal number that has been passed to the macro via its defined
preprocessor symbol.

example:
#define SYMBOL_ID_SUN 010
#define SYMBOL_ID_RAIN 011
#define SYMBOL_ID_FOG 012

#define CONVERT(arg) <whatever does the conversion>

char string[] = "the wether is " CONVERT(SYMBOL_ID_FOG) " today.";

should result in:

"the wether is \001\012 today"


I tried something like
#define CONVERT(arg) "\001"##str
or
#define CONVERT(arg) "\001"##'\'##arg
or
....
but most of the time got troubles because of the interpretation of the \ as
a linebreak.

any c-preprocessor expert here that can help??

thanks a lot,
klaus!


p.s.: if possible also reply to (e-mail address removed)
 
D

Dave Hansen

Hi all!
sorry, my first posting to this newsgroup - hope everything works fine...

I'm looking for a macro that converts an octal number assigned to a [...]

example:
#define SYMBOL_ID_SUN 010
#define SYMBOL_ID_RAIN 011
#define SYMBOL_ID_FOG 012

#define CONVERT(arg) <whatever does the conversion>

char string[] = "the wether is " CONVERT(SYMBOL_ID_FOG) " today.";

should result in:

"the wether is \001\012 today"

How about something like this:

---begin included file---

C:\Dave>type pp.c

#define Stringize(x) #x
#define SlashString(x) Stringize(\001\x)

#define Convert(x) SlashString(x)

#define SYMBOL_ID_FOG 012

char * s = "The weather is " Convert(SYMBOL_ID_FOG) " today";

C:\Dave>gcc -E pp.c
# 1 "pp.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "pp.c"
# 9 "pp.c"
char * s = "The weather is " "\001\012" " today";

C:\Dave>

--- End included file ---

Note that string literals separated only by whitespace get
concatenated.
p.s.: if possible also reply to (e-mail address removed)

As you can see, Google makes this... difficult. Hope it's helpful
anyway.

-=Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top