4

I have problem in converting to lower case letters for unicode characters in VC++ MFC .I have unicode characters in a CString Variable.so,with English MakeLower() works fine and I get lower case .But it cannot convert unicode characters to lower case.I did try the STL algorithm transform :

std::string data = "ИИИЛЛЛЛ"; //bulgerian chars

std::transform(data.begin(), data.end(), data.begin(), ::tolower);

but it fails to load the unicode chars ,I get "????" symbols in place of unicode chars .

Can you please let me know if there is a solution for unicode chars .I dont like to use boost libraries.Thanks in advance!

Basawaraj
  • 41
  • 1
  • 2

2 Answers2

3

If your project uses the Unicode Character Set (project properties), CString::MakeLower() should work -- note that this will not convert the contents of the string, it returns a new string, see this MSDN article:

CString s1(_T("ABC")), s2;
s2 = s1.MakeLower();
ASSERT(s2 == _T("abc"));   

EDIT: CString::MakeLower() does change the contentrs of the string, it also returns a reference to the converted string

Edward Clements
  • 5,040
  • 2
  • 21
  • 27
  • Thanks for your response,yes my project properties is set to "Use Unicode Character Set" .So ,I dont think MakeLower() will work with Unicode characters. – Basawaraj Jul 18 '13 at 08:03
  • it does work for me! (when the project is set to use Unicode Character Set), CString should point to CStringW which supports Unicode) -- see this [MSDN Article](http://msdn.microsoft.com/en-us/library/vstudio/ms174288.aspx), it says `A CString object supports either the char type or the wchar_t type. Which one it supports depends on which one of the symbols, MBCS or UNICODE, is defined at compile time` – Edward Clements Jul 18 '13 at 11:32
  • OK,Thanks .Found the solution .Calling setlocale before CString::MakeLower() works for uniocde lower case conversion.yes,may be if i set properties to MBCS ,it may work.Thanks – Basawaraj Jul 18 '13 at 12:08
1

Try

std::wstring data = L"ИИИЛЛЛЛ"; // Wide chars

std::transform(data.begin(), data.end(), data.begin(), std::tolower<wchar_t>);
MSalters
  • 173,980
  • 10
  • 155
  • 350
  • Hi MSalters Thanks Now i was able to load the unicode characters ,But even the algorithm fails to convert to lower case for unicode character set – Basawaraj Jul 18 '13 at 08:00
  • 2
    Thank you all guys ! I found the solution ,We need to set the Locale first before calling the algorith;Works fine now :) ::setlocale(LC_ALL,""); std::wstring data = L"ИЗПИТВАНЕ"; // Wide chars std::transform(data.begin(), data.end(), data.begin(), ::towlower); thank you so much MSalters :) – Basawaraj Jul 18 '13 at 09:03
  • well to add to this ,I also found that just calling setlocale() even fixes CString::MakeLower() to work with Unicode characters,no need to go for the STL algorithm.Now MakeLower() also works for Unicode characters. – Basawaraj Jul 18 '13 at 11:44
  • @Basawaraj this comment should be the accepted answer. Maybe you can answer you own question thus making the solution more visible. – Bojan Hrnkas Nov 18 '20 at 09:11