DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

ATL宏:

USES_CONVERSION;

W2A

A2W

 

CString StringUtil::UTF8_to_UNICODE(const char *utf8_string, int length)
{
    int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, utf8_string, length, NULL, 0);   
    wchar_t* wszString = new wchar_t[wcsLen + 1];
    ::MultiByteToWideChar(CP_UTF8, NULL, utf8_string, length, wszString, wcsLen);
    wszString[wcsLen] = '\0';
    CString unicodeText(wszString); 
    delete[] wszString;

    return unicodeText;
}

void StringUtil::UNICODE_to_UTF8(const CString& unicodeString, std::string& str)
{
    int stringLength = ::WideCharToMultiByte(CP_UTF8, NULL, unicodeString, wcslen(unicodeString), NULL, 0, NULL, NULL);

    char* buffer = new char[stringLength + 1];
    ::WideCharToMultiByte(CP_UTF8, NULL, unicodeString, wcslen(unicodeString), buffer, stringLength, NULL, NULL);
    buffer[stringLength] = '\0';

    str = buffer;

    delete[] buffer;
}

posted on 2014-08-10 23:20  DoubleLi  阅读(2148)  评论(0编辑  收藏  举报