?
Solved

C++ Hex to Decimal Conversion

Posted on 2007-04-05
2
Medium Priority
?
4,044 Views
Last Modified: 2013-11-26
I need to convert a hex number into decimal.  I'm using Visual Studio C++ 6.0 so it can be either C++ or C routine.  What should I use?  Is there a C++ routine available?
0
Comment
Question by:lcor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 7

Accepted Solution

by:
nixfreak earned 1000 total points
ID: 18862226
I won't attempt to provide an answer as this looks like homework (not allowed on EE) and is very easy to solve anyway. But you might be interested to know that internally decimals, octals, and hexadecimal numbers are all stored in the same binary format. Checkout the output of this program:

#include <stdio.h>

int main() {
int i = 0xFE;    /* hexadecimal value */

printf("%d %x %o", i, i, i);    /* printout the value of i in decimal, hex and octal */
}
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 1000 total points
ID: 18862619
If you want to convert a string representing a decimal number to an integer, then take a look at sscanf for C :

        http://www.cplusplus.com/reference/clibrary/cstdio/sscanf.html

(especially the %x type specifier)

For C++, you could use a stringstream with the hex stream manipulator :

        http://www.cplusplus.com/reference/iostream/stringstream/
        http://www.cplusplus.com/reference/iostream/manipulators/hex.html

Note that those pages contain a complete reference, including examples.
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question