Convert char* to wchar* in C

2020-02-09 07:25发布

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.

5条回答
唯我独甜
2楼-- · 2020-02-09 08:04

setlocale() followed by mbstowcs().

查看更多
Animai°情兽
3楼-- · 2020-02-09 08:10

If you happen to have the Windows API availiable, the conversion function MultiByteToWideChar offers some configurable string conversion from different encodings to UTF-16. That might be more appropriate if you don't care too much about portability and don't want to figure out exactly what the implications of different locale settings are to the string converison.

查看更多
甜甜的少女心
4楼-- · 2020-02-09 08:12

if you currently have ANSI chars. just insert an 0 ('\0') before each char and cast them to wchar_t*.

查看更多
爷、活的狠高调
5楼-- · 2020-02-09 08:16

Try swprintf with the %hs flag.

Example:

wchar_t  ws[100];
swprintf(ws, 100, L"%hs", "ansi string");
查看更多
虎瘦雄心在
6楼-- · 2020-02-09 08:22

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*

查看更多
登录 后发表回答