//string類型的utf-8字符串轉(zhuǎn)為CString類型的unicode字符串CString ConvertUTF8ToCString( std::string utf8str ){ /* 預(yù)轉(zhuǎn)換,得到所需空間的大小 */ int nLen = ::MultiByteToWideChar( CP_UTF8, NULL, utf8str.data(), utf8str.size(), NULL, 0 ); /* 轉(zhuǎn)換為Unicode */ std::wstring wbuffer; wbuffer.resize( nLen ); ::MultiByteToWideChar( CP_UTF8, NULL, utf8str.data(), utf8str.size(), (LPWSTR) (wbuffer.data() ), wbuffer.length() );#ifdef UNICODE return(CString( wbuffer.data(), wbuffer.length() ) );#else /* * 轉(zhuǎn)換為ANSI * 得到轉(zhuǎn)換后長度 */ nLen = WideCharToMultiByte( CP_ACP, 0, wbuffer.data(), wbuffer.length(), NULL, 0, NULL, NULL ); std::string ansistr; ansistr.resize( nLen ); /* 把unicode轉(zhuǎn)成ansi */ WideCharToMultiByte( CP_ACP, 0, (LPWSTR) (wbuffer.data() ), wbuffer.length(), (LPSTR) (ansistr.data() ), ansistr.size(), NULL, NULL ); return(CString( ansistr.data(), ansistr.length() ) );#endif}
//CString類型的unicode字符串轉(zhuǎn)為string類型的utf-8字符串string _UnicodeToUtf8(CString Unicodestr){ wchar_t* unicode = Unicodestr.AllocSysString(); int len; len = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, NULL, 0, NULL, NULL); char *szUtf8 = (char*)malloc(len + 1); memset(szUtf8, 0, len + 1); WideCharToMultiByte(CP_UTF8, 0, unicode, -1, szUtf8, len, NULL, NULL); string result = szUtf8; free(szUtf8); return result;}
//string轉(zhuǎn)const char*string str = "abc";const char* result= str.c_str();
//CString轉(zhuǎn)char*CString str;USES_CONVERSION;char * result= T2A(str);
//判斷字符是否為UTF-8編碼方式,是就返回true,否則返回falsebool _is_str_utf8(string src){ const char* str= src.c_str(); unsigned int nBytes = 0;//UFT8可用1-6個字節(jié)編碼,ASCII用一個字節(jié) unsigned char chr = *str; bool bAllAscii = true; for (unsigned int i = 0; str[i] != '\0'; ++i){ chr = *(str + i); //判斷是否ASCII編碼,如果不是,說明有可能是UTF8,ASCII用7位編碼,最高位標(biāo)記為0,0xxxxxxx if (nBytes == 0 && (chr & 0x80) != 0){ bAllAscii = false; } if (nBytes == 0) { //如果不是ASCII碼,應(yīng)該是多字節(jié)符,計算字節(jié)數(shù) if (chr >= 0x80) { if (chr >= 0xFC && chr <= 0xFD){ nBytes = 6; } else if (chr >= 0xF8){ nBytes = 5; } else if (chr >= 0xF0){ nBytes = 4; } else if (chr >= 0xE0){ nBytes = 3; } else if (chr >= 0xC0){ nBytes = 2; } else{ return false; } nBytes--; } } else{ //多字節(jié)符的非首字節(jié),應(yīng)為 10xxxxxx if ((chr & 0xC0) != 0x80){ return false; } //減到為零為止 nBytes--; } } //違反UTF8編碼規(guī)則 if (nBytes != 0) { return false; } if (bAllAscii){ //如果全部都是ASCII, 也是UTF8 return true; } return true;}
//string轉(zhuǎn)CStringstring str = "1234";CString result = CString(str.c_str());