Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# C++ Hex to Decimal Conversion

Posted on 2007-04-05
Medium Priority
4,045 Views
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
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

LVL 7

Accepted Solution

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

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

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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
Course of the Month10 days, 16 hours left to enroll