Win32 <tchar.h> TCHAR to UTF-8?

64 Views Asked by At

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.

1

There are 1 best solutions below

0
Remy Lebeau On

TCHAR maps to either char or wchar_t depending on whether UNICODE is defined during compiling.

If TCHAR is wchar_t, a wchar_t string is encoded in UTF-16 on Windows, so you can use WideCharToMultiByte() (or equivalent) with the CP_UTF8 codepage to convert a UTF-16 wchar_t string to a UTF-8 char string.

If TCHAR is char, you have to know ahead of time which locale the char string is encoded in, before you can correctly convert it to UTF-8. If you do not know the locale, you are SOL if the char string contains any non-ASCII characters. Otherwise, you can first use MultiByteToWideChar() (or equivalent) with an appropriate codepage to convert the char string to a UTF-16 wchar_t string, and then convert that to UTF-8 as above.