Solved

easiest way to get Decimal to Hex in c++?

Posted on 2004-10-04
6
399 Views
Last Modified: 2010-04-17
if anyone could write a quick code segment or explain the easiest way to convert decimal integers to hex in c++ that'd be great. thanks.
0
Comment
Question by:drumrboy44
[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
6 Comments
 
LVL 1

Expert Comment

by:Perfunction
ID: 12219902
0
 
LVL 11

Expert Comment

by:raj3060
ID: 12219995
Try this link:
http://www.gidforums.com/t-2615.html
They have very good comments and explanation.
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 12220178
you can use c++ sstream library. Something like:

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

// somewhere in your code
    int testnumber = 100;
    stringstream ss;   // this object will do the conversion

    ss << hex << testnumber;   // 'hex' will format number to hex format
    cout << ss.str();  // display result  str() method return the hex string, you can assign it to a 'string' object
0
Industry Leaders: 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!

 
LVL 57

Expert Comment

by:Julian Hansen
ID: 12220215
I am assuming you want to do this for display purposes - for any other purpose there is not really a conversion because 0xFF = 255 = 11111111.

Output to the console

printf ( "%x", decimalnumber) ;

save to a string

sprintf ( str, "%x", decimalnumber ) ;
0
 
LVL 2

Expert Comment

by:Ruskialt
ID: 12224390
If your decimal number (and integer) is in a string first convert the number to an int, then format the variable to another string

char pInteger[] = "256"; //my integer string
char pHex[256]; //destination string
int nValue;

if( sscanf( pInteger , "%d" , &nValue ) == 1 ) //returns number of converted elements
{
   sprintf( pHex , "0x%08x" , nValue );
}

=> pHex now holds "0x00000100"

CString has a Format() function that doest excactly what sprintf() does, in case you use MFC. Also if your source string is in a CString, you can use it as the first parameter in the sscanf() statement, because CString's can be seen as const char*'s

leave the "0x", and the "08" if you dont want the prefix and the 0-padding and the 8 characters wide field
0
 
LVL 57

Expert Comment

by:Julian Hansen
ID: 12224567
Just a minor point - if you are going to convert from a string you could also use atoi or atol

char dec[] = "1234" ;
char hex[10] ;
int x ;

x = atoi ( str ) ;
sprintf ( hex, "0x%08x", x ) ; // adopting Ruskialt's formatting options here


0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help Required 3 133
statistics basics sources 2 73
Data Analysis 7 59
Looking for reading material 2 41
This article will show, step by step, how to integrate R code into a R Sweave document
In this post we will learn different types of Android Layout and some basics of an Android App.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

732 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