参考了网上一些方法:所谓的短字符,就是用8bit来表示的字符,典型的应用是ASCII码. 而宽字符,顾名思义,就是用16bit表示的字符,典型的有UNICODE.
1. ASCII to Unicode(CP_ACP) wstring ASCIIToUNICODE(char cArry[]) //传入参数为ANSI串,即用char数组或者string表示的串 wchar_t *pTemp = new wchar_t[nLen]; //new一个wchar_t空间,保存Unicode串 2. Unicode to ASCII(CP_ACP) string UNICODEToASCII(wchar_t cArry[]) //传入参数为Unicode串,用“wchar_t cArry[] = {L"这是个测试"};”表示
3. UTF-8 to Unicode(CP_UTF8) wstring UTF8ToUnicode( const string& str ) { int unicodeLen = ::MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0 ); wchar_t *pUnicode = new wchar_t[unicodeLen]; memset(pUnicode, 0, unicodeLen*sizeof(wchar_t)); ::MultiByteToWideChar( CP_UTF8, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen ); wstring rt = pUnicode; if(pUnicode ) { delete [] pUnicode ; pUnicode = NULL; } return rt; } 4. Unicode to UTF-8(CP_UTF8) string UnicodeToUTF8( const wstring& str ) { // wide char to multi char int iTextLen = ::WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, NULL, 0, NULL, NULL ); char *pElementText= new char[iTextLen]; memset(pElementText, 0, iTextLen); ::WideCharToMultiByte( CP_UTF8, 0, str.c_str(), -1, pElementText, iTextLen, NULL, NULL ); string strText; strText = pElementText; if(pElementText) { delete [] pElementText; pElementText = NULL; } return strText; }
|
|