C-Preprocessor Experts wanted...

Discussion in 'C Programming' started by Klaus Siegesleitner, Mar 8, 2007.

  1. 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
     
    Klaus Siegesleitner, Mar 8, 2007
    #1
    1. Advertising

  2. Klaus Siegesleitner

    Dave Hansen Guest

    On Mar 8, 4:42 pm, "Klaus Siegesleitner" <> wrote:
    > 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


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

    -=Dave
     
    Dave Hansen, Mar 9, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David F. Skoll
    Replies:
    2
    Views:
    875
    David F. Skoll
    Nov 14, 2003
  2. Harvey
    Replies:
    0
    Views:
    736
    Harvey
    Jul 16, 2004
  3. Harvey
    Replies:
    1
    Views:
    868
    Daniel
    Jul 16, 2004
  4. =?Utf-8?B?ZGF2ZQ==?=

    datetime convert experts...help wanted

    =?Utf-8?B?ZGF2ZQ==?=, Jun 1, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    1,772
    Mark Rae
    Jun 2, 2006
  5. Wim Hoogenraad

    Perl experts wanted

    Wim Hoogenraad, Jul 29, 2005, in forum: Perl Misc
    Replies:
    1
    Views:
    94
    Tad McClellan
    Jul 29, 2005
Loading...

Share This Page