# Converting Hex RGB Strings to a DWORD

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

## 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 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 >= '0' && hex <= '9') {      result += int(hex - '0');    } else if (hex >= 'A' && hex <= 'F') {      result += int(hex - 'A') + 10;    } else if (hex >= 'a' && hex <= 'f') {      result += int(hex - '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 on other sites
DWORD dw = strtoul(string, NULL, 16);

##### 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 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>='0' && in<='9' || in>='A' && in<='F') ? ( (in>='A' && in<='F') ? in-'A'+10 : in-'0') : 0; }

##### 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 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 on other sites
Quote:
 Original post by Emmanuel DelogetHi,bool from_hex_to_int(const std::string& hex, int& result){ result = 0; for (int i=0; i= '0' && hex <= '9') { result += int(hex - '0'); } else if (hex >= 'A' && hex <= 'F') { result += int(hex - 'A') + 10; } else if (hex >= 'a' && hex <= 'f') { result += int(hex - '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

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 11
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
632924
• Total Posts
3009232
• ### Who's Online (See full list)

There are no registered users currently online

×