Created
August 18, 2020 20:38
-
-
Save charlie-x/388b1abb436b79c4e75d88637bd64cf2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CStringA ConvertUnicodeToUTF8(const CStringW& uni) | |
{ | |
if (uni.IsEmpty()) return ""; // nothing to do | |
CStringA utf8; | |
int cc = 0; | |
// get length (cc) of the new multibyte string excluding the \0 terminator first | |
if ((cc = WideCharToMultiByte(CP_UTF8, 0, uni, -1, NULL, 0, 0, 0) - 1) > 0) | |
{ | |
// convert | |
char* buf = utf8.GetBuffer(cc); | |
if (buf) WideCharToMultiByte(CP_UTF8, 0, uni, -1, buf, cc, 0, 0); | |
utf8.ReleaseBuffer(); | |
} | |
return utf8; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment