473,657 Members | 2,504 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 15734
"^_^" <cl********@hot mail.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.wa nted. 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_PE R_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.l earn.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
5163
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
62095
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 supposed to write this function? String.replace(/</g,'&lt;');
17
5031
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 ( http://simiax.com/ihier.html ) however it is $199 and a demo to test is not available. Plus they're not answering emails :( Are there any other applications that will do this? Also, does any know or care to guesstimate the size of the final static HTML...
4
59990
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 have looked at the System.Xml namespace, but I can't quite find what I need. XmlTextWriter.WriteString() does the type of encoding I need, but it writes the string. I just want it to return the encoded string. Can someone tell me the class and...
5
3540
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 it. The problem is that i recive this from external source and cant
1
1422
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 exactly the opposite. Thanks, Loui Mercieca
2
10162
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 this? HTML code
1
82847
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
1618
by: vaidas gudas | last post by:
how to convert HTML page to XML format
3
1817
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 functionality? Thank you! Regards, Mickey
0
8844
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8742
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8518
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8621
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7354
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1971
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.