2010年4月9日星期五

C++中的标准string由unicode转换为ansi[windows环境]

#include <windows.h>

string utf2ansi(const string &src){
    int nLen = MultiByteToWideChar( CP_UTF8, 0, src.data(), -1, NULL, NULL );
    //得到UTF8编码的字符串长度
    LPWSTR lpwsz = new WCHAR[nLen];
    MultiByteToWideChar( CP_UTF8, 0, src.data(), -1, lpwsz, nLen ); 

    int nLen1 = WideCharToMultiByte( CP_ACP, 0, lpwsz, nLen, NULL, NULL, NULL, NULL );
    LPSTR lpsz = new CHAR[nLen1];
    WideCharToMultiByte( CP_ACP, 0, lpwsz, nLen, lpsz, nLen1, NULL, NULL );
    string str(lpsz);
    delete []lpsz;
    return str;
}

没有评论:

发表评论