How can I convert a Win32 TCHAR string from <tchar.h> to a UTF-8 string? I haven't found any documentation how to do that, only TCHAR to a multibyte string (wcstombs()), which relies on the current locale.
I cannot use setlocale() etc, as I write a DLL.
I tried TCHAR to char with wcstombs(), but that relies on the current locale, and does not always give a UTF8 string. I need a solution which does not use the current locale.
TCHARmaps to eithercharorwchar_tdepending on whetherUNICODEis defined during compiling.If
TCHARiswchar_t, awchar_tstring is encoded in UTF-16 on Windows, so you can useWideCharToMultiByte()(or equivalent) with theCP_UTF8codepage to convert a UTF-16wchar_tstring to a UTF-8charstring.If
TCHARischar, you have to know ahead of time which locale thecharstring is encoded in, before you can correctly convert it to UTF-8. If you do not know the locale, you are SOL if thecharstring contains any non-ASCII characters. Otherwise, you can first useMultiByteToWideChar()(or equivalent) with an appropriate codepage to convert thecharstring to a UTF-16wchar_tstring, and then convert that to UTF-8 as above.