码迷,mamicode.com
首页 > 编程语言 > 详细

Visual C++ unicode and utf8 转换

时间:2014-08-11 00:17:31      阅读:364      评论:0      收藏:0      [点我收藏+]

标签:des   io   art   ar   amp   new   c++   har   

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

Visual C++ unicode and utf8 转换,布布扣,bubuko.com

Visual C++ unicode and utf8 转换

标签:des   io   art   ar   amp   new   c++   har   

原文地址:http://www.cnblogs.com/lidabo/p/3903616.html

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