### Parsing hex value

Hi, guys!

I got a trouble about how to convert a hex value to a string...
Here :
 ``12`` ``````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

 ``12345678`` ``````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

 ``12345678`` ``````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 :
 ``1234`` ``````DWORD dw = 0x12AB; std::string str = Convert(dw); //str = "0x12AB" ``````

Is there any method which can do this?
Last edited on

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

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 >>
 ``123456`` ``````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.