Parsing hex value

Hi, guys!

I got a trouble about how to convert a hex value to a string...
Here :
1
2
DWORD dw = 0xFFAADDED;
std::string str = Convert(dw);


Do you know, guys?
what should the string look like?

for example what should 0xA become?
10,ten,1010,...?
Use the family of standard functions std::to_string
EDIT: or use sttd::ostringstream. For example


1
2
3
4
5
6
7
8
std::ostringstream os;
unsigned int x = 0xFFAADDED;

os << std::showbase << std::hex << std::uppercase << x;

std::string s = os.str();

std::cout << s << std::endl;


or

1
2
3
4
5
6
7
8
std::ostringstream os;
unsigned int x = 0xFFAADDED;

os << "0x" << std::hex << std::uppercase << x;

std::string s = os.str();

std::cout << s << std::endl;


Also you can use C function sprintf.
Last edited on
Darkmaster wrote:
what should the string look like?

for example what should 0xA become?
10,ten,1010,...?


Here is my expected output :
1
2
3
4
DWORD dw = 0x12AB;
std::string str = Convert(dw);

//str = "0x12AB" 

Is there any method which can do this?
Last edited on
how about modulo16 and right-shift?
how about modulo16 and right-shift?


I don't know & understand those methods. Simply I have no idea....
Last edited on
@vlad from moscow

Thanks you, but my compiler actually doesn't support ostringstream.
But the function sprintf...How to print hex value properly?
what i meant is
%16 and >>
1
2
3
4
5
6
char s[12] = { '\0' };
unsigned int x = 0xFFAADDED;

sprintf( s, "%#X", x );

std::cout << s << std::endl;


Or you can use more safe function snprintf instead of sprintf.

To use std::ostringstream you have to include header <sstream>

EDIt: if your compiler does not support format specifier '#" then you can write

sprintf( s, "0x%X", x );
Last edited on
Wow. It's working now.
Vlad from moscow, thanks a bunch!!
Topic archived. No new replies allowed.