What is the difference between WideCharToMultiByte() and wcstombs() When to use which one?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
WideCharToMultiByte is a Windows API function that converts between Windows defined multibyte code pages stored in CHAR, and UTF16, stored in WCHAR. The codepage to use is passed as the first parameter, and can be passed as CP_ACP, which means a codepage specific to the systems current locale - set in the control panel Localization tool "Language to use for Non Unicode Programs". It is accessed by #including , and is available only on Windows.
wcstombs is a Standard C Runtime function that converts between the c-runtimes current char* encoding, and wchar_t* encoding. setlocale iirc can be used to set the codepage(s) to use.
std::codecvt is the C++ Standard Library template class, in , used for converting strings between various encodings using a variety of traits type mechanisims to define the source and destination encodings.
There are other libraries, including ICONV or ICU that also do various unicode <-> multibyte conversions.
wcstombs()
is portable, whereas theWideCharToMultiByte()
function is win32 only.When it comes down to it,
wcstombs()
calls a system-specific function, which on Win32 will most likely be a straight call toWideCharToMultiByte()
- however, it might bypass this function completely and just go straight to the internals.In any case, there's no practical difference.
In a nutshell: the
WideCharToMultiByte
function exposes the encodings/code pages used for the conversion in the parameter list, whilewcstombs
does not. This is a major PITA, as the standard does not define what encoding is to be used to produce thewchar_t
, while you as a developer certainly need to know what encoding you are converting to/from.Apart from that,
WideCharToMultiByte
is of course a Windows API function and is not available on any other platform.Therefore I would suggest using
WideCharToMultiByte
without a moment's thought if your application is not specifically written to be portable to non-Windows OSes. Otherwise, you might want to wrestle withwcstombs
or (preferably IMHO) look into using a full-feature portable Unicode library such as ICU.Like with any other function: use the function that does what you need in your program.
WideCharToMultiByte
converts from UTF-16 (used as Win32 WCHAR representation) to Win32 code-page of your choice.wcstombs
converts from implementation-defined internalwchar_t
representation to current implementation-defined internal multi-byte representation.So if your program is native Win32 program that uses lots of WIN32 API functions that use and return WCHAR strings then you need
WideCharToMultiByte
. If you write some functions based on standard library (not Win32 API) that work with standard C wchar_t strings then you needwcstombs
.The main difference is that
wcstombs
is a standard function, so use that if code needs to run on any platform other than Windows.