首页 > 代码库 > Unicode与ANSI的转换

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;
}

 

Unicode与ANSI的转换