c++中unicode字符串如何表示

w_vc_love 2010-11-05 01:18:29
如果用std::string str="abcd";那么如何将str转换成unicode字符串
请赐教 谢谢!
...全文
384 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
老邓 2010-11-05
  • 打赏
  • 举报
回复
#include <windows.h>
#include <iostream>
#include <vector>

using namespace std;

std::wstring UT2WC(const char* buf)
{
int len = MultiByteToWideChar(CP_UTF8, 0, buf, -1, NULL, 0);
std::vector<wchar_t> unicode(len);
MultiByteToWideChar(CP_UTF8, 0, buf, -1, &unicode[0], len);

return std::wstring(&unicode[0]);
}

std::string WC2UT(const wchar_t* buf)
{
int len = WideCharToMultiByte(CP_UTF8, 0, buf, -1, NULL, 0, NULL, NULL);
std::vector<char> utf8(len);
WideCharToMultiByte(CP_UTF8, 0, buf, -1, &utf8[0], len, NULL, NULL);

return std::string(&utf8[0]);
}

std::wstring MB2WC(const char* buf)
{
int len = MultiByteToWideChar(CP_ACP, 0, buf, -1, NULL, 0);
std::vector<wchar_t> unicode(len);
MultiByteToWideChar(CP_ACP, 0, buf, -1, &unicode[0], len);

return std::wstring(&unicode[0]);
}

std::string WC2MB(const wchar_t* buf)
{
int len = WideCharToMultiByte(CP_ACP, 0, buf, -1, NULL, 0, NULL, NULL);
std::vector<char> utf8(len);
WideCharToMultiByte(CP_ACP, 0, buf, -1, &utf8[0], len, NULL, NULL);

return std::string(&utf8[0]);
}

int main()
{
setlocale(LC_ALL, "");

const wchar_t* s1 = L"UNICODE转换成UTF-8";
cout << WC2UT(s1).c_str() << endl;

const char* s2 = "ANSI转换成UNICODE";
wcout << MB2WC(s2).c_str() << endl;

const wchar_t* s3 = L"UNICODE转换成ANSI";
cout << WC2MB(s3).c_str() << endl;

return 0;
}
libinfei8848 2010-11-05
  • 打赏
  • 举报
回复
MultiByteToWideChar方法转换把
Erorr 2010-11-05
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 w_vc_love 的回复:]

为什么VC 不自己检查如果是unicode就把string 改为wstring 就像GetWindowText有GetWindowTextA
和GetWindowTextW两种,而程序员不必关心具体是哪一种。
[/Quote]
UNICODE和ASCII相比,占用空间x2,而且由于历史原因,在语言标准上需要同时支持两种字符的操作
这已经够用了,如果用户需要封装什么宏,可以根据自己喜好,例如Windows提供的那样
但语言标准要尽可能简单,另外这也是人制定的,就像C++本身没有多线程库一样
hastings 2010-11-05
  • 打赏
  • 举报
回复

#ifndef UNICODE
typedef std::string Tstring;
#else
typedef std::wstring Tstring;
#endif

Tstring str(_T("中文"));
wyfwx 2010-11-05
  • 打赏
  • 举报
回复
mfc 可以用 _T("abcd")
luciferisnotsatan 2010-11-05
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 w_vc_love 的回复:]

为什么VC 不自己检查如果是unicode就把string 改为wstring 就像GetWindowText有GetWindowTextA
和GetWindowTextW两种,而程序员不必关心具体是哪一种。
[/Quote]
应为basic_string<>这是标准库的东西。
你要用CString的话,那就没关系了。这是微软自己的东西
typedef ATL::CStringT< wchar_t, StrTraitMFC_DLL< wchar_t > > CStringW;
typedef ATL::CStringT< char, StrTraitMFC_DLL< char > > CStringA;
typedef ATL::CStringT< TCHAR, StrTraitMFC_DLL< TCHAR > > CString;
cobra_chen 2010-11-05
  • 打赏
  • 举报
回复
wchar_t ?
w_vc_love 2010-11-05
  • 打赏
  • 举报
回复
为什么VC 不自己检查如果是unicode就把string 改为wstring 就像GetWindowText有GetWindowTextA
和GetWindowTextW两种,而程序员不必关心具体是哪一种。
healer_kx 2010-11-05
  • 打赏
  • 举报
回复
wstring a = L"你好";
luciferisnotsatan 2010-11-05
  • 打赏
  • 举报
回复
用 std::wstring

typedef basic_string<char, char_traits<char>, allocator<char> >
string;
typedef basic_string<wchar_t, char_traits<wchar_t>,
allocator<wchar_t> > wstring;
healer_kx 2010-11-05
  • 打赏
  • 举报
回复
L"abcd"

L被很多非 VC编译器也支持的。

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧