码迷,mamicode.com
首页 > 其他好文 > 详细

Unicode与ANSI的转换

时间:2017-06-30 21:05:08      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:text   set   str   ems   ring   amp   sizeof   nic   rto   

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的转换

标签:text   set   str   ems   ring   amp   sizeof   nic   rto   

原文地址:http://www.cnblogs.com/chaguang/p/7100515.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!