Is there a method to convert a hex value to a color value, even in the form of a COLORREF if needs be.
how do i convert: #FFFF00 to 255,255,0
Surely there must be a method/class to do this
thanks
Will
Is there a method to convert a hex value to a color value, even in the form of a COLORREF if needs be.
how do i convert: #FFFF00 to 255,255,0
Surely there must be a method/class to do this
thanks
Will
Hex-to-RGB Conversion
CommanderG
Here is a C# implementation of the convertion:
private
string HexToRgb(string hex){
int r, g, b; int num = (int) long.Parse(hex, NumberStyles.HexNumber);r = (num & 0xFF0000) >> 16;
g = (num & 0xFF00) >> 8;
b = num & 0xFF;
return string.Format("{0}, {1}, {2}", r, g, b);}
AlBriggs
Is #FFFF00 a string Is the output again a string String to COLORREF
Give us more information what kind of conversion do you need.
And no! There is no such class or function. You can convert a hex string into a long with strtol. The lower 3 bytes are than the COLORREF value.
dotnetwizkid
zetto
Ok, yes #FFFF00 is a string and i would like to run a conversion method on it that will return a string looking like this "255,255,0"
Is there no conversion even in the .net framework
Thanks Martin
Will
BatDeveloper
Because hexadecimal uses base-16. And the third parameter to strtol is the base. Honestly, you should just look up these functions on MSDN, they are very well documented.
Sergey Bereznikov
Will, it can't be a problem to splitt a string! This group does not exist to tell you the most basics of programming!
CString strColor(_T(#ffff00");
// Get in groups of 2 hex chars, and for get the leadin #
DWORD dwColor = 0;
for (int i=0; i<3; ++i)
{
dwColor << 8;
dwColor |= strtol(strColor.Mid(i*2+1,2),NULL,16);
}
ASSERT(dwColor==0xffff00);
CString strOut;
strOut.Format(_T("%d,%d,%d"),
LOBYTE(HIWORD(dwColor)),
HIBYTE(LOWORD(dwColor)),
LOBYTE(LOWORD(dwColor)));
NetSoft
Try something like this :-
void Convert(LPCTSTR input, LPTSTR output)
{
LPTSTR pStop;
int num = _tcstol(input + 1, &pStop, 16);
int b1 = (num & 0xFF0000) >> 16;
int b2 = (num & 0xFF00) >> 8;
int b3 = num & 0xFF;
_stprintf(output, _T("%d, %d, %d"), b1, b2, b3);
}
LPCTSTR input = _T("#FF1B2C");
TCHAR output[12] = {0};
Convert(input, output);
tomtodd
Your input string contains hex, and you want an output string containing decimal equivalents. There's no direct function to do that - you'll have to write your own.
Strip out the three Hex bytes from the input string, convert the hex byte strings to integers, and then use sprintf or CString::Format to write those integers to the output string.
Mick Lang
Ok how do you strip out the three hex bytes With the exception of the "#" is it a case of the first 2 letters, then the middle two and then the last two
And once you've done this how do you convert the hex byte strings to integers Is there a method for this
Thanks
Will
Rod77
Yeah i know i just want to understand is all. I do know how to split a string!
so for the string "#FFFF00"
The rgb values, still in string form would be this
R = "FF"
G = "FF"
B = "00"
and so strtol method is the converter method Why are you using 16 as the base out of curiosity
Thanks
HerbD
Ok thank you fellas
Will