Converting Hex RGB Strings to a DWORD

Started by
6 comments, last by Enigma 19 years, 4 months ago
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
Advertisement
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]
DWORD dw = strtoul(string, NULL, 16);
-Mike
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
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; }

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

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]
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
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 >= '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

This topic is closed to new replies.

Advertisement