正文

string to wstring?2008-08-23 18:27:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/lqf110/37816.html

分享到:

unix下的汉字处理问题 如何把一个汉字作为一个字符来处理?在以前,似乎比较麻烦,因为一个汉字一向是由2个字符来表示的。比较汉字,往往变成了字符串的比较。unicode出现之后,情况就好多了,每个汉字都有唯一的编码,从此汉字就可以作为单个字符来对待了。 stl提供了string类来处理字符串,但是针对的是单字节字符串。如果想处理汉字,可以选择wstring。用法和string完全相同,但是处理的是宽字符。string到wstring之间的转换,似乎stl没有提供好的方法,所以还得用c的库函数来处理。以下给出一段代码,演示在unix下面,处理汉字的方法 #include <iostream>#include <string>#include <list>#include <stdlib.h>#include <locale.h> namespace std {} using namespace std; int main(){  int cnt;  wchar_t wcs[100], wc;    string myword="列表内容为:";    setlocale(LC_CTYPE, "");  //很重要,没有这一句,转换会失败    mbstowcs(wcs, myword.c_str(), 99);    wstring newword(wcs);    cout<<"string content is:"<<myword.c_str()<< endl;       cout<<"wstring size is:"  <<newword.size()<<endl;    return 0;}

阅读(7874) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册