醉了,windows下宏定義了不少char類型 LPTSTR 。今天,直接使用,qt報錯,真TM費事。html
將「CPU」轉化爲wcha_t *編程
- QString str = "CPU";
- const wchar_t * str_cpu = reinterpret_cast<const wchar_t *>(str.utf16());
下面這篇blog很好windows
QT QString, wchar_t *, TCHAR, CString和其餘字符或字符串類型的轉化 數組
點擊打開連接安全
- const wchar_t * encodedName = reinterpret_cast<const wchar_t *>(fileName.utf16());
-
- QByteArray fileName = QFile::encodeName(aFileName);
- const char * encodedName = fileName.constData();
-
- const char * tmp = str.toUtf8().constData();
- [/code]
- Windows 數據類型: http:
- [code lang="cpp"]
- #ifdef UNICODE
- typedef wchar_t TCHAR;
- #else
- typedef char TCHAR;
- #endif
-
- #ifdef UNICODE
- typedef LPCWSTR LPCTSTR;
- #else
- typedef LPCSTR LPCTSTR;
- #endif
-
- typedef const char * LPCSTR;
-
- typedef const wchar_t * LPCWSTR;
-
- QString text(QString::fromUtf16(reinterpret_cast<const unsigned short *>(tmp)));
- 另外一種解決辦法是使用QString::fromWCharArray(),但這個函數可能致使一些還沒有解決的wchar_t符號問題。
-
- 最佳的編程風格: 使用L來定義wchar_t寬字符串,好比 L"text" 字義了一個UNICODE字符串"text"。
-
- 今天又看到一個文章,關於字符串之間的轉換,比較全面,在此將英文翻譯並整理一下。
- 原文地址:http:
-
- QString與其餘字符類型之間的轉換,QString在Qt4中是UNICODE編碼的,使用utf16規範。
-
- QString::fromAscii ( const char * str, int size = -1 );
- QString::fromLatin1 ( const char * str, int size = -1 );
- QString::fromLocal8Bit ( const char * str, int size = -1 );
- QString::fromRawData ( const QChar * unicode, int size );
- QString::fromStdString ( const std::string & str );
- QString::fromStdWString ( const std::wstring & str );
- QString::fromUcs4 ( const uint * unicode, int size = -1 );
- QString::fromUtf8 ( const char * str, int size = -1 );
- QString::fromUtf16 ( const ushort * unicode, int size = -1 );
- QString::fromWCharArray ( const wchar_t * string, int size = -1 );
-
- QString::toStdString () ;
- QString::toStdWString ();
-
- BSTR bstr_str;
- QString q_str((QChar*)bstr_str, wcslen(bstr_str));
- bstr_str = SysAllocString(q_str.utf16());
-
- QString::toLocal8Bit().constData();
- QString::fromLocal8Bit ( const char * str, int size = -1 );
-
- QString::utf16();
- QString::fromUtf16 ( const ushort * unicode, int size = -1 );
-
- CString c_str(qstring::utf16());
- QString fromUtf16 (LPCTSTR(c_str) );
- CString轉換爲char*
-
- CString cstr(asdd);
- const char* ch = (LPCTSTR)cstr;
-
- CString cstr = "ASDDSD";
- char *ch = cstr.GetBuffer(cstr1.GetLength() + 1);
- cstr.ReleaseBuffer();
-
- CString cstr1 = "ASDDSD";
- int strLength = cstr1.GetLength() + 1;
- char *pValue = new char[strLength];
- strncpy(pValue, cstr1, strLength);
-
- CString cstr2 = "ASDDSD";
- int strLength1 = cstr1.GetLength() + 1;
- char chArray[100];
- memset(chArray,0, sizeof(bool) * 100);
- strncpy(chArray, cstr1, strLength1);
-
- CString origCString("Hello, World!");
- wchar_t* wCharString = origCString.GetBuffer(origCString.GetLength()+1);
- size_t origsize = wcslen(wCharString) + 1;
- size_t convertedChars = 0;
- char *CharString;
- CharString=new char(origsize);
- wcstombs_s(&convertedChars, CharString, origsize, wCharString , _TRUNCATE);
- cout << CharString << endl;
- 從UTF8編碼到GB編碼的字符串轉換方法:
-
- QString Utf8_To_GB(QString strText)
- {
- return QString::fromUtf8(strText.toLocal8Bit().data());
- }
- 從GB編碼到UTF8編碼的字符串轉換方法:
-
- QString GB_To_Utf8(char *strText)
- {
- return QString::fromLocal8Bit(strText);
- }
對了還有一些QT的 QString char int之間的轉換。函數
這篇點擊打開連接ui
還有一篇,也不錯。Qt中 QString 和int, char等的「相互」轉換 點擊打開連接編碼
http://blog.csdn.net/qq61394323/article/details/28391579spa