Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert char* to wchar* in C

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.

like image 908
Crupuk Avatar asked Jan 28 '11 08:01

Crupuk


People also ask

What is the difference between Wchar and char?

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.

What does wchar_t mean in C?

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.

How many bytes is Wchar?

On Windows x86 and x64, a wchar_t type character always contains 2 bytes.

What is Wchar data type?

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.


3 Answers

Try swprintf with the %hs flag.

Example:

wchar_t  ws[100];
swprintf(ws, 100, L"%hs", "ansi string");
like image 115
Nick Dandoulakis Avatar answered Sep 18 '22 05:09

Nick Dandoulakis


setlocale() followed by mbstowcs().

like image 25
user541686 Avatar answered Sep 21 '22 05:09

user541686


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*

like image 31
Franky Rivera Avatar answered Sep 19 '22 05:09

Franky Rivera