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

?

^_^

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?
 
B

Ben Pfaff

^_^ said:
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?
 
K

Kevin Goodsell

^_^ said:
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
 
T

Thomas Matthews

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

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

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top