Jump to content
  • Advertisement
Sign in to follow this  
DeathRay2K

Casting from DWORD to a string of a hex

This topic is 3855 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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!