Unicode与ANSI的转换

string UnicodeToANSI(const wstring& str)
{
    char *pStr;
    int iwstrLen = WideCharToMultiByte(CP_ACP, 0, str.c_str(), -1, 0, 0, 0, 0);
    cout << "iwstrlen=" << iwstrLen << endl;
    pStr = new char[iwstrLen +1];
    memset(pStr, 0, sizeof(char)*(iwstrLen + 1));
    WideCharToMultiByte(CP_ACP, 0, str.c_str(), -1, pStr, iwstrLen, 0, 0);
    string strText;
    strText = pStr;
    delete pStr;
    cout << "转换ANSI完成" << endl;
    return strText;
}

wstring ANSItoUnicode(const string& str)
{
    WCHAR *pwstr;
    int istrLen = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, 0, 0);
    cout << "istrlen=" << istrLen << endl;
    pwstr = new WCHAR[istrLen + 1];
    memset(pwstr, 0, (istrLen + 1) * sizeof(WCHAR));
    MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, pwstr, istrLen);
    wstring wTemp = pwstr;
    delete pwstr;
    cout << "转换Unicode完成" << endl;
    return wTemp;
}

 

posted @ 2017-06-30 20:05  菜鸟也有高飞的时候  阅读(1398)  评论(0编辑  收藏  举报