Skip to content

Instantly share code, notes, and snippets.

@charlie-x
Created August 18, 2020 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charlie-x/388b1abb436b79c4e75d88637bd64cf2 to your computer and use it in GitHub Desktop.
Save charlie-x/388b1abb436b79c4e75d88637bd64cf2 to your computer and use it in GitHub Desktop.
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