As your CString contains a series of wchar_t
, you can just use WideCharToMultiByte
with the output charset as CP_UTF8
. The function will return the number of bytes written to the output buffer, or the length of the UTF-8 encoded string
LPWSTR instr;
char outstr[MAX_OUTSTR_SIZE];
int utf8_len = WideCharToMultiByte(CP_UTF8, 0, instr, -1, outstr, MAX_OUTSTR_SIZE, NULL, NULL);
If you don't need the output string, you can simply set the output buffer size to 0
cbMultiByte
Size, in bytes, of the buffer indicated by lpMultiByteStr. If this parameter is set to 0, the function returns the required buffer size for lpMultiByteStr and makes no use of the output parameter itself.
In that case the function will return the number of bytes in UTF-8 without really outputting anything
int utf8_len = WideCharToMultiByte(CP_UTF8, 0, instr, -1, NULL, 0, NULL, NULL);
If your CString is really CStringA
, i.e. _UNICODE
is not defined, then you need to use MultiByteToWideChar
to convert the string to UTF-16 and then convert from UTF-16 to UTF-8 with WideCharToMultibyte
. See How do I convert an ANSI string directly to UTF-8? But new code should never be compiled without Unicode support anyway