如何将char *转换为wchar_t *? [英] How to convert char* to wchar_t*?

查看:259
本文介绍了如何将char *转换为wchar_t *?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过实现这样的函数,但不幸的是它不工作:

  const wchar_t * GetWC const char * c)
{
const size_t cSize = strlen(c)+1;
wchar_t wc [cSize];
mbstowcs(wc,c,cSize);

return wc;
}

我的主要目标是能够在Unicode中集成正常的字符串应用。非常感谢您提供的任何建议。

解决方案

使用 std :: wstring 而不是C99可变长度数组。当前标准保证 std :: basic_string 的连续缓冲区。例如,

  std :: wstring wc(cSize,L'#'); 
mbstowcs(& wc [0],c,cSize);

C ++不支持C99可变长度数组,因此如果将代码编译为纯C ++,



这样,你的函数返回类型也应该是 std :: wstring p>

请务必在 main 中设置相关区域设置。



setlocale(LC_ALL,)



hth。,


I've tried implementing a function like this, but unfortunately it doesn't work:

const wchar_t *GetWC(const char *c)
{
    const size_t cSize = strlen(c)+1;
    wchar_t wc[cSize];
    mbstowcs (wc, c, cSize);

    return wc;
}

My main goal here is to be able to integrate normal char strings in a Unicode application. Any advice you guys can offer is greatly appreciated.

解决方案

Use a std::wstring instead of a C99 variable length array. The current standard guarantees a contiguous buffer for std::basic_string. E.g.,

std::wstring wc( cSize, L'#' );
mbstowcs( &wc[0], c, cSize );

C++ does not support C99 variable length arrays, and so if you compiled your code as pure C++, it would not even compile.

With that change your function return type should also be std::wstring.

Remember to set relevant locale in main.

E.g., setlocale( LC_ALL, "" ).

Cheers & hth.,

这篇关于如何将char *转换为wchar_t *?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆