在C中将char *转换为wchar *

时间:2021-11-16 20:14:01

I would like to convert a char* string to a wchar* string in C.

我想在C中将char *字符串转换为wchar *字符串。

I have found many answers, but most of them are for C++. Could you help me?

我找到了很多答案,但大多数答案都是针对C ++的。你可以帮帮我吗?

Thanks.

5 个解决方案

#1


15  

Try swprintf with the %hs flag.

尝试使用%hs标志的swprintf。

Example:

wchar_t  ws[100];
swprintf(ws, 100, L"%hs", "ansi string");

#2


3  

setlocale() followed by mbstowcs().

setlocale()后跟mbstowcs()。

#3


1  

what you're looking for is

你要找的是

mbstowcs

works just like the copy function from char* to char*

就像从char *到char *的复制功能一样

but in this case you're saving into a wchar_t*

但是在这种情况下你要保存到wchar_t *

#4


-1  

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.

如果您碰巧使用了Windows API,则转换函数MultiByteToWideChar提供了一些可配置的字符串转换,从不同的编码转换为UTF-16。如果您不太关心可移植性并且不想确切知道不同语言环境设置对字符串转换的影响,那么这可能更合适。

#5


-2  

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

如果你目前有ANSI字符。只需在每个char之前插入一个0('\ 0')并将它们转换为wchar_t *。

#1


15  

Try swprintf with the %hs flag.

尝试使用%hs标志的swprintf。

Example:

wchar_t  ws[100];
swprintf(ws, 100, L"%hs", "ansi string");

#2


3  

setlocale() followed by mbstowcs().

setlocale()后跟mbstowcs()。

#3


1  

what you're looking for is

你要找的是

mbstowcs

works just like the copy function from char* to char*

就像从char *到char *的复制功能一样

but in this case you're saving into a wchar_t*

但是在这种情况下你要保存到wchar_t *

#4


-1  

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.

如果您碰巧使用了Windows API,则转换函数MultiByteToWideChar提供了一些可配置的字符串转换,从不同的编码转换为UTF-16。如果您不太关心可移植性并且不想确切知道不同语言环境设置对字符串转换的影响,那么这可能更合适。

#5


-2  

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

如果你目前有ANSI字符。只需在每个char之前插入一个0('\ 0')并将它们转换为wchar_t *。