std::wstring CPostData::UTF8ToUnicode(char* UTF8) { DWORD dwUnicodeLen; //转换后Unicode的长度 TCHAR *pwText; //保存Unicode的指针 //std::string strUnicode; //返回值 //获得转换后的长度,并分配内存 dwUnicodeLen = MultiByteToWideChar(CP_UTF8,0,UTF8,-1,NULL,0); pwText = new TCHAR[dwUnicodeLen]; if (!pwText) { std::wstring strUnicode1; return strUnicode1; } //转为Unicode MultiByteToWideChar(CP_UTF8,0,UTF8,-1,pwText,dwUnicodeLen); //转为CString //strUnicode.Format(_T("%s"),pwText); std::wstring strUnicode(pwText); //清除内存 delete []pwText; //返回转换好的Unicode字串 return strUnicode; }
std::wstring MBToUnicode(const std::string &strMB) { int nLen = MultiByteToWideChar(CP_ACP, 0, strMB.c_str(), strMB.size(), NULL, 0); if(nLen <= 0) { return NULL; } WCHAR *pStrUnicode = new WCHAR[nLen + 1]; if(pStrUnicode == NULL) { return NULL; } MultiByteToWideChar(CP_ACP, 0, strMB.c_str(), strMB.size(), pStrUnicode, nLen); pStrUnicode[nLen] = 0; if(pStrUnicode[0] == 0xFEFF) { for(int i = 0; i < nLen; i++) { pStrUnicode[i] = pStrUnicode[i+1]; } } std::wstring strUnicode(pStrUnicode); delete [] pStrUnicode; return std::move(strUnicode); }
void StringTestCase::Extraction() { wxString s(wxT("Hello, world!")); CPPUNIT_ASSERT( wxStrcmp( s.c_str() , wxT("Hello, world!") ) == 0 ); CPPUNIT_ASSERT( wxStrcmp( s.Left(5).c_str() , wxT("Hello") ) == 0 ); CPPUNIT_ASSERT( wxStrcmp( s.Right(6).c_str() , wxT("world!") ) == 0 ); CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , wxT("lo, w") ) == 0 ); CPPUNIT_ASSERT( wxStrcmp( s.Mid(3).c_str() , wxT("lo, world!") ) == 0 ); CPPUNIT_ASSERT( wxStrcmp( s.substr(3, 5).c_str() , wxT("lo, w") ) == 0 ); CPPUNIT_ASSERT( wxStrcmp( s.substr(3).c_str() , wxT("lo, world!") ) == 0 ); #if wxUSE_UNICODE static const char *germanUTF8 = "Oberfl\303\244che"; wxString strUnicode(wxString::FromUTF8(germanUTF8)); CPPUNIT_ASSERT( strUnicode.Mid(0, 10) == strUnicode ); CPPUNIT_ASSERT( strUnicode.Mid(7, 2) == "ch" ); #endif // wxUSE_UNICODE wxString rest; #define TEST_STARTS_WITH(prefix, correct_rest, result) \ CPPUNIT_ASSERT_EQUAL(result, s.StartsWith(prefix, &rest)); \ if ( result ) \ CPPUNIT_ASSERT_EQUAL(correct_rest, rest) TEST_STARTS_WITH( wxT("Hello"), wxT(", world!"), true ); TEST_STARTS_WITH( wxT("Hello, "), wxT("world!"), true ); TEST_STARTS_WITH( wxT("Hello, world!"), wxT(""), true ); TEST_STARTS_WITH( wxT("Hello, world!!!"), wxT(""), false ); TEST_STARTS_WITH( wxT(""), wxT("Hello, world!"), true ); TEST_STARTS_WITH( wxT("Goodbye"), wxT(""), false ); TEST_STARTS_WITH( wxT("Hi"), wxT(""), false ); #undef TEST_STARTS_WITH rest = "Hello world"; CPPUNIT_ASSERT( rest.StartsWith("Hello ", &rest) ); CPPUNIT_ASSERT_EQUAL("world", rest); #define TEST_ENDS_WITH(suffix, correct_rest, result) \ CPPUNIT_ASSERT_EQUAL(result, s.EndsWith(suffix, &rest)); \ if ( result ) \ CPPUNIT_ASSERT_EQUAL(correct_rest, rest) TEST_ENDS_WITH( wxT(""), wxT("Hello, world!"), true ); TEST_ENDS_WITH( wxT("!"), wxT("Hello, world"), true ); TEST_ENDS_WITH( wxT(", world!"), wxT("Hello"), true ); TEST_ENDS_WITH( wxT("ello, world!"), wxT("H"), true ); TEST_ENDS_WITH( wxT("Hello, world!"), wxT(""), true ); TEST_ENDS_WITH( wxT("very long string"), wxT(""), false ); TEST_ENDS_WITH( wxT("?"), wxT(""), false ); TEST_ENDS_WITH( wxT("Hello, world"), wxT(""), false ); TEST_ENDS_WITH( wxT("Gello, world!"), wxT(""), false ); #undef TEST_ENDS_WITH }
wstring gbk_to_utf16( const string& strSrc ) { wstring strUnicode(strSrc.size(), 0); const string strOldLoc = setlocale(LC_CTYPE, NULL); setlocale(LC_CTYPE, ".936"); size_t unicodeLength = mbstowcs(const_cast<wchar_t*>(strUnicode.data()), strSrc.c_str(), strSrc.size()); setlocale(LC_CTYPE, strOldLoc.c_str()); if (unicodeLength == -1) return L"___________mbcs to utf16 failed_______YBTXDlg____"; strUnicode.resize(unicodeLength); return strUnicode; }