How do I use _wcstombs_l function to convert a UNICODE string to UTF-8? I guess I need to pass in a UTF-8 _locale_t, but I don't know how to create a variable of this type.
-
first result on google working example -[http://msdn.microsoft.com/en-us/library/5d7tc9zw.aspx] – Leon Nov 01 '13 at 16:52
-
Please read my question - I want to convert to UTF-8. – nim Nov 01 '13 at 17:09
1 Answers
You can't - UTF-8 is not really an ANSI code page, there's no locale that uses it. Use WideCharToMultiByte(CP_UTF8, ...)
instead.
You are supposed to create a _locale_t
argument for _wcstombs_l
with _create_locale
function. Its documentation states, and I quote:
The locale argument can take a locale name, a language string, a language string and country/region code, a code page, or a language string, country/region code, and code page. The set of available locale names, languages, country/region codes, and code pages includes all that are supported by the Windows NLS API except the code pages that require more than two bytes per character - for example, UTF-7 and UTF-8. If you provide a code page like UTF-7 or UTF-8,
_create_locale
will fail and returnNULL
.
Emphasis mine.

- 50,461
- 4
- 56
- 85
-
Okay... This means I had a misunderstanding of this convenient function. :-( – nim Nov 03 '13 at 10:29
-
I came across this question: http://stackoverflow.com/questions/7859638/stumped-with-unicode-boost-c-codecvts. I think it's quite possible to convert from/to UTF-8 using the _locale_t parameter, but don't know how to create the locale without using boost. – nim Nov 04 '13 at 05:38
-
1`std::locale` and `_locale_t` are two different beasts. The latter is a Microsoft-specific invention for use with Microsoft-specific C-style library calls like `_wcstombs_l`. You create one with a (needless to say) Microsoft-specific function `_create_locale`. – Igor Tandetnik Nov 04 '13 at 13:59
-