Sign in to follow this  

Casting from DWORD to a string of a hex

This topic is 3490 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
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[i].strrep) << hex << pixels[i].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[i].strrep;
stringify << hex << pixels[i].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[i].strrep


Then copy the stringstream's string into your string:

stringstream stringify = pixels[i].strrep;
stringify << hex << pixels[i].col
pixels[i].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
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

This topic is 3490 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this