Convert char* to wchar* in C

时间秒杀一切 提交于 2019-11-29 03:47:24

Try swprintf with the %hs flag.

Example:

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

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 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.

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!