Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

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

Posted on 2004-10-04
Medium Priority
409 Views
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
Question by:drumrboy44

LVL 1

Expert Comment

ID: 12219902
0

LVL 11

Expert Comment

ID: 12219995
http://www.gidforums.com/t-2615.html
They have very good comments and explanation.
0

LVL 55

Accepted Solution

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

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

using namespace std;

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

LVL 61

Expert Comment

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

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 61

Expert Comment

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

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Why WooCommerce is one of the majorly favored choices when it comes to having an eCommerce store. This article will acquaint you with some reasons that I believe make it one of the best eCommerce platforms available.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Six Sigma Control Plans
###### Suggested Courses
Course of the Month11 days, 16 hours left to enroll