当前位置:C++技术网 > 资讯 > string系列:6 string和wstring相互转换以及wstring显示中文问题

string系列:6 string和wstring相互转换以及wstring显示中文问题

更新时间:2016-10-14 17:33:04浏览次数:1+次

    如果你只是使用C++来处理字符串,会用到string。不过string是窄字符串ASCII,而很多Windows API函数用的是宽字符Unicode。这样让string难以应对。作为中国的程序员,我们第一个想到的字符串就是中文,而不是英文。所以经常会遇到中文字符问题,需要经常在ASCII字符串和Unicode字符串转换。而C++的string并么有很好的去支持这么一个转换,所以还需要我们自己去写代码转换。
    而这个过程就是比较痛苦的。我遇到了我写一遍,你遇到了又写一遍,所以,大家总会感觉C++程序难,这里就是一个方面。STL提供的字符串类string不完善,所以让人很难受。所以,现在我写好了,就分享出来,供大家直接使用。
    以下是程序运行的效果:
C++的string和wstring相互转换以及wstring显示中文问题
    而在写这个函数的过程中,意外发现wcout不能直接显示中文,真是够坑的。我们需要进行本地化设置,调用函数setlocale(),就可以了。在稍后给出的测试代码中有,就不单独写出来了。当然,如果你不想使用这个函数,也没有关系,你可以将wstring转为string,就可以正常输出中文了。
    下面先给出string转到wstring和wstring转string的函数代码,含头文件:
#include <comdef.h>
string wstring2string(wstring wstr)
{
    string result;
    int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);
    if( len <= 0 )return result;
    char* buffer = new char[len + 1];
    if(buffer == NULL )return result;
    WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL);
    buffer[len] = '\0';
    result.append(buffer);
    delete[] buffer;
    return result;
}
wstring string2wstring(string str)
{
    wstring result;
    int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), NULL, 0);
    if( len < 0 )return result;
    wchar_t* buffer = new wchar_t[len + 1];
    if( buffer == NULL )return result;
    MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), buffer, len);
    buffer[len] = '\0';
    result.append(buffer);
    delete[] buffer;
    return result;
}

    函数名称就可以看出函数的功能了。此转换利用了Windows的字符集转换函数WideCharToMultiByte和MultiByteToWideChar。不过可以感觉到,看到这两个函数那么多参数,心里还是蛮凉快的,哈哈哈。没关系,这里已经写好了代码,很欣慰吧。两个函数使用特别简单,一个输入一个输出。
    下面就是完整的测试代码:
#include <iostream>
using namespace std;
#include <comdef.h>
string wstring2string(wstring wstr)
{
    string result;
    int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);
    if( len <= 0 )return result;
    char* buffer = new char[len + 1];
    if(buffer == NULL )return result;
    WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL);
    buffer[len] = '\0';
    result.append(buffer);
    delete[] buffer;
    return result;
}
wstring string2wstring(string str)
{
    wstring result;
    int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), NULL, 0);
    if( len < 0 )return result;
    wchar_t* buffer = new wchar_t[len + 1];
    if( buffer == NULL )return result;
    MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), buffer, len);
    buffer[len] = '\0';
    result.append(buffer);
    delete[] buffer;
    return result;
}
void main()
{
    //string
    string str="string:C++技术网www.cjjjs.com";
    cout<<str.c_str()<<endl;
    //wstring
    wstring wstr = L"wstring:C++技术网www.cjjjs.com";
    setlocale( LC_ALL, "chs" );//设置wcout输出中文
    wcout<<wstr.c_str()<<endl;

    //将wstring转为string来显示中文
    cout<<"wstr2str:"<<wstring2string(wstr).c_str()<<endl;

    //将string转为wstring
    wstring wstr2 = string2wstring(str);
    wcout<<"str2wstr:"<<wstr.c_str()<<endl;

    int i;
    cin>>i;
}