Converting a Decimal number to Hexadecimal

Is there a pre-defined Library function that allows one to convert a Decimal number (of type unsigned int) to Hexadecimal?

Or will I have to make a function to do the math manually?
Use a stringstream and the hex manipulator. Search for those in this website's Reference.
If the type is already an unsigned int, then you're maybe you should be asking how to display it in hexadecimal format?

1
2
3
4
5
6
7
8
#include <iostream>
#include <iomanip> // for hex, dec
using namespace std;

unsigned int value = 1024;

// hex switches to hexadecimal display mode, dec back to decimal
cout << "value = " << hex << value << " / " << dec << value << endl;


Or if you need to write luddite code, use the %x format specifier with the print family of functions.
Last edited on
Actually I wanted to store it in a string.
I get the converting to string by stringstream part.
I can have that in the form of a string, but how can I store it in a variable?
stringstream also works the other way around!
stringstream also works the other way around!
As I can understand the Hex manipulator works with cout

So how can I store it in a string??

Figured it out. Thanks for the help
Last edited on
Topic archived. No new replies allowed.