在 C 中将 char* 转换为 wchar*
我想在 C 中将 char*
字符串转换为 wchar*
字符串。
我找到了很多答案,但大多数答案都是针对 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试使用
%hs
标志的swprintf
。例子:
Try
swprintf
with the%hs
flag.Example:
setlocale()
后跟mbstowcs()< /代码>
。
setlocale()
followed bymbstowcs()
.您正在寻找的功能
就像从 char* 到 char* 的复制功能一样,
但在这种情况下,您将保存到 wchar_t* 中
what you're looking for is
works just like the copy function from char* to char*
but in this case you're saving into a wchar_t*
如果您碰巧有可用的 Windows API,则转换函数 MultiByteToWideChar 提供了一些从不同编码到 UTF-16 的可配置字符串转换。如果您不太关心可移植性并且不想确切地弄清楚不同区域设置对字符串转换的影响,那么这可能更合适。
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.
如果您当前有 ANSI 字符。只需在每个字符之前插入 0 ('\0') 并将它们转换为 wchar_t* 即可。
if you currently have ANSI chars. just insert an 0 ('\0') before each char and cast them to wchar_t*.