By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,305 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

C-Preprocessor Experts wanted...

P: n/a
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 kl***@klausix.net



Mar 8 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Mar 8, 4:42 pm, "Klaus Siegesleitner" <k...@klausix.netwrote:
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 k...@klausix.net
As you can see, Google makes this... difficult. Hope it's helpful
anyway.

-=Dave

Mar 9 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.