^_^ 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