473,385 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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?

Nov 14 '05 #1
3 15721
"^_^" <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
^_^ 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
^_^ 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: haoren | last post by:
Can anybody help me with this problem: How can I echo a string that contain <? and <?php? For example, $str="test <? and <?php echo"; echo $str;
4
by: higabe | last post by:
Three questions 1) I have a string function that works perfectly but according to W3C.org web site is syntactically flawed because it contains the characters </ in sequence. So how am I...
17
by: DesignGuy | last post by:
I would like to download the RDF dump and generate static HTML pages (with customizable headers and footers). I have only found one program called iHierarchy that claims to do this (...
4
by: joe_rattz | last post by:
I need to convert a text string ("Dewey & Cheatham & Howe") to an XML encoded string ("Dewey &amp; Cheatham &amp; Howe"). I am not building an XML document, I am just trying to convert a single string. I...
5
by: tobbe | last post by:
Hi Im trying to load a XmlDataDocument with the following xml: <ROOT> <NAME> &LT; &AMP; &GT; " '</NAME> </ROOT> And i know I have a entity problem here, but i cant find any solution for...
1
by: Loui Mercieca | last post by:
Hi, How do you convert from an encoded stream ( ex. &lt; ) to xml (ex. < ). I need a reliable sytem ( not by replacing text ). I have found a method: XmlTextWriter.WriteString Method(), which does...
2
by: Jasonkimberson | last post by:
I am doing a data pull of HTML from a database that will be put into a drop down menu currently after i pull and populate the information, it converts my < into &lt; whats the work around for...
1
by: Paul Ritchie | last post by:
Is there something in .NET that will convert a string so that it can be displayed correctly as HTML? eg I would like to convert the string "a < b" to "a C; b". cheers, Paul.
1
by: vaidas gudas | last post by:
how to convert HTML page to XML format
3
by: mickeydev | last post by:
Hi All, I am looking for an API that accepts a string and converts the characters into their corresponding escape char (i.e. < into &lt; & into &amp;). Anyone who have used an API with such...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.