Dec 18, 2012 at 10:54am
what should the string look like?
for example what should 0xA become?
10,ten,1010,...?
Dec 18, 2012 at 10:56am
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 Dec 18, 2012 at 11:30am
Dec 18, 2012 at 11:05am
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 Dec 18, 2012 at 11:06am
Dec 18, 2012 at 11:11am
how about modulo16 and right-shift?
Dec 18, 2012 at 11:36am
how about modulo16 and right-shift? |
I don't know & understand those methods. Simply I have no idea....
Last edited on Dec 18, 2012 at 11:36am
Dec 18, 2012 at 11:39am
@vlad from moscow
Thanks you, but my compiler actually doesn't support ostringstream.
But the function sprintf...How to print hex value properly?
Dec 18, 2012 at 11:44am
what i meant is
%16 and >>
Dec 18, 2012 at 12:04pm
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 Dec 18, 2012 at 12:14pm
Dec 18, 2012 at 12:28pm
Wow. It's working now.
Vlad from moscow, thanks a bunch!!