Casting from DWORD to a string of a hex

Started by
12 comments, last by Brother Bob 15 years, 11 months ago
Basically, I have DWORD colour values that I need to convert into a hexadecimal and then a string, using C++. I have no idea what to use to do this. My first try involved stringstream(stringversion) << hex << theDWORD but that just gave me an empty string...
Advertisement
If you inset ios::hex into the stream that should work. Can you post some actual code?
Sure, here's what I have:
stringstream(pixels.strrep) << hex << pixels.col

pixels is an array with a struct containing a DWORD 'col' and a string 'strrep'.
How are you trying to get the string out of that? It looks like you're creating the stringstream as a temporary.
Quote:Original post by DeathRay2K
Sure, here's what I have:
stringstream(pixels.strrep) << hex << pixels.col

pixels is an array with a struct containing a DWORD 'col' and a string 'strrep'.


That stringstream you're inserting characters into is a temporary object. How do you plan to refer to it later in order to extract your newly formatted string?

Do something like this:

stringstream stringify = pixels.strrep;
stringify << hex << pixels.col
cout << stringify.str() << endl;
I'm not trying to output the value, I'm trying to put it in pixels.strrep
Quote:Original post by DeathRay2K
I'm not trying to output the value, I'm trying to put it in pixels.strrep


Then copy the stringstream's string into your string:

stringstream stringify = pixels.strrep;
stringify << hex << pixels.col
pixels.strrep = stingify.str();

... or something similar.

[Edited by - fpsgamer on May 24, 2008 7:42:17 PM]
Alright, I've tried that, but I get 3 errors:
Quote:
1>c:\users\garnet\code\shmup\particl\d2kimage.cpp(51) : error C2440: 'initializing' : cannot convert from 'std::string' to 'std::basic_stringstream<_Elem,_Traits,_Alloc>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]
1> Constructor for class 'std::basic_stringstream<_Elem,_Traits,_Alloc>' is declared 'explicit'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]
1>c:\users\garnet\code\shmup\particl\d2kimage.cpp(53) : error C2065: 'stingify' : undeclared identifier
1>c:\users\garnet\code\shmup\particl\d2kimage.cpp(53) : error C2228: left of '.str' must have class/struct/union
1> type is ''unknown-type''
stringstream might have an explicit constructor.

Initialize it this way:

stringstream stringify(pixels.strrep);
It works great, thank you very much!

This topic is closed to new replies.

Advertisement