• Advertisement
Sign in to follow this  

Casting from DWORD to a string of a hex

This topic is 3563 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
If you inset ios::hex into the stream that should work. Can you post some actual code?

Share this post


Link to post
Share on other sites
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'.

Share this post


Link to post
Share on other sites
How are you trying to get the string out of that? It looks like you're creating the stringstream as a temporary.

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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''

Share this post


Link to post
Share on other sites
stringstream might have an explicit constructor.

Initialize it this way:

stringstream stringify(pixels.strrep);

Share this post


Link to post
Share on other sites
DWORD color = RGB( 255, 0, 255 );
char buffer[32];
sprintf( buffer, "%X", color );

Share this post


Link to post
Share on other sites
Yeah sure, go ahead and suggest an ugly stone-age C-style solution when a working C++ solution has already been offered. :P

Share this post


Link to post
Share on other sites
Yeah i will !
keep it simple,
what's important is readability,
and imo stringstream is a much more uglier solution.

Share this post


Link to post
Share on other sites
Quote:
Original post by Molle85
Yeah i will !
keep it simple,
what's important is readability,
and imo stringstream is a much more uglier solution.

Keyword there being YOUR opinion. One which I, personally, don't share with you.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement