网站首页 > 物联资讯 > 技术分享

Visual C++ unicode and utf8 转换

2016-09-28 00:00:00 广州睿丰德信息科技有限公司 阅读
睿丰德科技 专注RFID识别技术和条码识别技术与管理软件的集成项目。质量追溯系统、MES系统、金蝶与条码系统对接、用友与条码系统对接

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

RFID管理系统集成商 RFID中间件 条码系统中间层 物联网软件集成