Solved

C++ Hex to Decimal Conversion

Posted on 2007-04-05
2
4,035 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
2 Comments
 
LVL 7

Accepted Solution

by:
nixfreak earned 250 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 250 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MYSQL responding very slow 3 51
Regarding swagger API 1 39
I need help setting focus to an @Html.TextBoxFor in my MVC.Net application 9 15
Data organization issue 7 40
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

696 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