Thank eleven.
I am using CString for containing "#20C0FF" in MFC.
How to convert that?
Thanks,
Main Topics
Browse All TopicsI have a string which contain a color : #20C0FF
How can I convert this string to type COLORREF ?
Thanks,
Lovenet
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
what exactly is the error message?
//if CString contains ANSI chars:
std::string str( cstrng.GetBuffer(8) );
cstring.ReleaseBuffer() ;
std::istringstream stm(str) ;
char c ; COLORREF clr ;
stm >> c >> std::hex >> clr ; // if string has colorref value
//if CString contains UNICODE chars:
std::wstring wstr( cstrng.GetBuffer(8) );
cstring.ReleaseBuffer() ;
std::wistringstream stm(wstr) ;
char c ; COLORREF clr ;
stm >> c >> std::hex >> clr ; // if string has colorref value
You could try this to extract the RGB values
string str = "#b235ff";
const char *s1 = str.c_str() + 5;
int l=0;
if( *s1 >= 0x30 && *s1 <= 0x39 )
l = *s1 & 0x0f;
else
l = (*s1 & 0x0f) + 9;
printf("Red %d\n", l );
s1 = str.c_str() + 3;
l=0;
if( *s1 >= 0x30 && *s1 <= 0x39 )
l = *s1 & 0x0f;
else
l = (*s1 & 0x0f) + 9;
printf("Green %d\n", l );
s1 = str.c_str() + 1;
l=0;
if( *s1 >= 0x30 && *s1 <= 0x39 )
l = *s1 & 0x0f;
else
l = (*s1 & 0x0f) + 9;
printf("Blue %d\n", l );
Best Regards,
DeepuAbrahamK
>>>> use CString::GetBuffer(int) to retrieve the buffer;
No, CString has a built-in cast operator to const char*, you don't need the GetBuffer which makes a writeable buffer.
// case not UNICODE
CString color = "#20C0FF" ;
std::istringstream stm((const char*)color) ; // here the cast operator applies
char c ; COLORREF clr ;
stm >> c >> std::hex >> clr ; // if string has colorref value
int r, g, b ;
stm >> c >> hex >> std::setw(2) >> r >> std::setw(2) >> g >> std::setw(2) >> b ;
clr = RGB(r,g,b) ; // if string has rgb values
Same is for UNICODE CStrings where the cast operator turns a CString to a const wchar_t*.
>>>> I tried but it is noticed error ???
You need
#include <sstream>
#include <iomanip>
to compile the code snippet.
Regards, Alex
Business Accounts
Answer for Membership
by: eleven_squaredPosted on 2007-05-27 at 07:57:55ID: 19164430
std::string color = "#20C0FF" ;
std::istringstream stm(color) ;
char c ; COLORREF clr ;
stm >> c >> std::hex >> clr ; // if string has colorref value
int r, g, b ;
stm >> c >> hex >> std::setw(2) >> r >> std::setw(2) >> g >> std::setw(2) >> b ;
clr = RGB(r,g,b) ; // if string has rgb values