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

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

P: n/a
^_^
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?

Nov 14 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"^_^" <cl********@hotmail.com> 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
Nov 14 '05 #2

P: n/a
^_^ 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.
Nov 14 '05 #3

P: n/a
^_^ 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

Nov 14 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.