I would like to convert a char*
string to a wchar*
string in C.
I have found many answers, but most of them are for C++. Could you help me?
Thanks.
char is used for so called ANSI family of functions (typically function name ends with A ), or more commonly known as using ASCII character set. wchar_t is used for new so called Unicode (or Wide) family of functions (typically function name ends with W ), which use UTF-16 character set.
The wchar_t type is an implementation-defined wide character type. In the Microsoft compiler, it represents a 16-bit wide character used to store Unicode encoded as UTF-16LE, the native character type on Windows operating systems.
On Windows x86 and x64, a wchar_t type character always contains 2 bytes.
The WCHAR data type contains a 16-bit Unicode character. C++ Copy. #if ! defined(_NATIVE_WCHAR_T_DEFINED) typedef unsigned short WCHAR; #else typedef wchar_t WCHAR; #endif.
Try swprintf
with the %hs
flag.
Example:
wchar_t ws[100];
swprintf(ws, 100, L"%hs", "ansi string");
setlocale()
followed by mbstowcs()
.
what you're looking for is
mbstowcs
works just like the copy function from char* to char*
but in this case you're saving into a wchar_t*
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With