How to convert HEX <-> DEC constant value in C source file?

Discussion in 'C Programming' started by ^_^, Jan 6, 2004.

  1. ^_^

    ^_^ Guest

    How to convert HEX <-> DEC constant value in C source file, and write out
    the result c files?

    a=4;
    b=6;

    from or to


    a=0x4;
    b=0x6;

    howto?
    ^_^, Jan 6, 2004
    #1
    1. Advertising

  2. ^_^

    Ben Pfaff Guest

    "^_^" <> writes:

    > How to convert HEX <-> DEC constant value in C source file, and write out
    > the result c files?
    >
    > a=4;
    > b=6;
    >
    > from or to
    >
    >
    > a=0x4;
    > b=0x6;


    Are you saying that you want to modify C source files, converting
    all hexadecimal constants into decimal constants or vice versa?
    --
    "I'm not here to convince idiots not to be stupid.
    They won't listen anyway."
    --Dann Corbit
    Ben Pfaff, Jan 7, 2004
    #2
    1. Advertising

  3. ^_^ wrote:
    > How to convert HEX <-> DEC constant value in C source file, and write out
    > the result c files?
    >
    > a=4;
    > b=6;
    >
    > from or to
    >
    >
    > a=0x4;
    > b=0x6;
    >
    > howto?
    >


    This is still not very well-explained. It sounds like you are asking how
    to parse a C source file, changing hex literals to decimal literals of
    vice-versa, and output the modified file. The answer is (as is so often
    the case): "Write a program that does it." If you have a specific
    question on how to write that program, ask in an appropriate group. This
    is not comp.sources.wanted. This is a group for discussion of the C
    language, not a place to ask for C source code.

    Incidentally, changing the base of integer literals in a C program could
    cause subtle changes to the meaning of the program, since the type of
    the literal may be different.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Jan 7, 2004
    #3
  4. ^_^ wrote:
    > How to convert HEX <-> DEC constant value in C source file, and write out
    > the result c files?
    >
    > a=4;
    > b=6;
    >
    > from or to
    >
    >
    > a=0x4;
    > b=0x6;
    >
    > howto?
    >


    Constants should be left in a base that is appropriate for the
    given context.

    For example:
    /* 1 */ #define MAX_CHICKENS_PER_ROAD 0x64
    /* 2 */ #define MAX_TURKEYS_PER_ROAD 45

    In the line #1, which is defining a constant for the
    maximum chickens that can cross a road, hexadecimal is not
    very intuituive. However, decimal is more appropriate
    in this context as in line 2.

    The issue is that you may not want all of the numbers converted.
    The task of selectively choosing which numbers not to
    convert is more difficult that converting all of them.

    You will have to parse the file and determine where a number
    is. Read it in then print it out in text format. You will
    have to decide about comments and format specifiers (perhaps
    also numbers as part of identifiers). This is not a simple
    task, but it can be done. By the way, this is good evidence
    for using named constants rather than magic numbers.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    Thomas Matthews, Jan 9, 2004
    #4
    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. Replies:
    10
    Views:
    6,191
    Neredbojias
    Aug 19, 2005
  2. ^_^
    Replies:
    2
    Views:
    374
    Ben Pfaff
    Jan 6, 2004
  3. ^_^
    Replies:
    1
    Views:
    532
    David Harmon
    Jan 7, 2004
  4. ^_^
    Replies:
    2
    Views:
    481
    Ben Pfaff
    Jan 6, 2004
  5. whatluo
    Replies:
    7
    Views:
    465
    whatluo
    Jun 11, 2005
Loading...

Share This Page