Sign in to follow this  

Converting Hex RGB Strings to a DWORD

This topic is 4748 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

Hello All Could someone please tell me how to convert a hex RGB color value stored in a string to an DWORD value ? Would I first have to seperate the color components? Basically I just need to know how to convert from Hex String to Base 10 DWORD. Thank You

Share this post


Link to post
Share on other sites
Hi,


bool from_hex_to_int(const std::string& hex, int& result)
{
result = 0;
for (int i=0; i<hex.size(); i++) {
// value *= 16; <--- I'M BADA55 O_o
result *= 16;
if (hex[i] >= '0' && hex[i] <= '9') {
result += int(hex[i] - '0');
} else if (hex[i] >= 'A' && hex[i] <= 'F') {
result += int(hex[i] - 'A') + 10;
} else if (hex[i] >= 'a' && hex[i] <= 'f') {
result += int(hex[i] - 'a') + 10;
} else {
return false;
}
}
return true;
}


Hope this is not some kind of homework... :)

< edit: corrected a VERY BAD ERROR in the code... boys! why didn't anybody tell me about it! :'( >
< edit II: edit strikes back. Thanks to Enigma. He just pointed out how idiot i am... gosh, i'm a badass ;_;)

Yours,

[Edited by - Emmanuel Deloget on December 16, 2004 3:40:19 PM]

Share this post


Link to post
Share on other sites
And to complete the collection (unless anyone has any more):
#include <sstream>

DWORD hexStringToDword(const std::string& string)
{
std::stringstream ss;
unsigned int uint;
ss << string;
ss >> std::hex >> uint;
if (ss.fail())
{
throw std::runtime_error("hexadecimal string expected");
}
return uint;
}


Enigma

Share this post


Link to post
Share on other sites
about as obfuscated as I could get it, assumes the hex digit is always valid and only upper case.

string in;
int out=0;

for(int i=0;i<6;++){ out*=16; out+=( in[i]>='0' && in[i]<='9' || in[i]>='A' && in[i]<='F') ? ( (in[i]>='A' && in[i]<='F') ? in[i]-'A'+10 : in[i]-'0') : 0; }

Share this post


Link to post
Share on other sites
I think I managed to get a little more obfuscated:
DWORD out=0xFFFFFFF0;
char t;
while((out & 0xF0000000) && *in){ out<<=4; t = *(in++);out+=((t>47 & t<58) | (t>64 & t<71)) ? ( (t>57) ? t-55 : t-48) : 0;}
DWORD mask=out>>28<<28;
while(out & mask){out &= ~mask;mask>>=4;};
return out;


Same assumptions apply.

EDIT: A slight "improvement". Also nice neat line wrapping (required a few type changes):
long o=~15;int t;while(o>>31&&*i){o
<<=4;t=*i++;o+=t>47&t<58|t>64&t<71?
t>57?t-55:t-48:0;}long m=o>>28<<28;
while(o&m){o&=~m;m>>=4;}return o;


I think I'll leave it at that.

Enigma

[Edited by - Enigma on December 16, 2004 1:09:43 PM]

Share this post


Link to post
Share on other sites
Woah, all the replies!

Thanks Guys, you helped me out alot. This isn't a homework assignment,im just trying to read hex colors from an xml file for a gui system i'm creating.

Thanks Again

Share this post


Link to post
Share on other sites
Quote:
Original post by Emmanuel Deloget
Hi,

bool from_hex_to_int(const std::string& hex, int& result)
{
result = 0;
for (int i=0; i<hex.size(); i++) {
value *= 16;
if (hex[i] >= '0' && hex[i] <= '9') {
result += int(hex[i] - '0');
} else if (hex[i] >= 'A' && hex[i] <= 'F') {
result += int(hex[i] - 'A') + 10;
} else if (hex[i] >= 'a' && hex[i] <= 'f') {
result += int(hex[i] - 'a') + 10;
} else {
return false;
}
}
return true;
}


Hope this is not some kind of homework... :)

< edit: corrected a VERY BAD ERROR in the code... boys! why didn't anybody tell me about it! :'( >

Yours,


Sorry, I meant to mention it but forgot. By the way, you've still got an error.

Enigma

Share this post


Link to post
Share on other sites

This topic is 4748 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