//QString to wchar_t *: const wchar_t * encodedName = reinterpret_cast<const wchar_t *>(fileName.utf16()); //QString to char * given a file name: QByteArray fileName = QFile::encodeName(aFileName); const char * encodedName = fileName.constData(); //Valid as long as fileName exists //QString to char * (general case): const char * tmp = str.toUtf8().constData(); [/code] Windows 数据类型: http://msdn.microsoft.com/en-us/library/aa383751.aspx [code lang="cpp"] //TCHAR: #ifdef UNICODE typedef wchar_t TCHAR; #else typedef char TCHAR; #endif //LPCTSTR: #ifdef UNICODE typedef LPCWSTR LPCTSTR; #else typedef LPCSTR LPCTSTR; #endif //LPCSTR: typedef const char * LPCSTR; //LPCWSTR: typedef const wchar_t * LPCWSTR; //LPCWSTR to QString: QString text(QString::fromUtf16(reinterpret_cast<const unsigned short *>(tmp))); 另一种解决办法是使用QString::fromWCharArray(),但这个函数可能导致一些尚未解决的wchar_t符号问题。 最佳的编程风格: 使用L来定义wchar_t宽字符串,比如 L"text" 字义了一个UNICODE字符串"text"。 今天又看到一个文章,关于字符串之间的转换,比较全面,在此将英文翻译并整理一下。 原文地址:http://hi.baidu.com/koko200147/blog/item/7e3cad828c9b9bb66d8119cb.html 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 ->std::string QString::toStdString () ; QString::toStdWString (); //BSTR<->QString,不太了解BSTR是什么,还没用到过,所以不知道对不对 BSTR bstr_str; QString q_str((QChar*)bstr_str, wcslen(bstr_str)); bstr_str = SysAllocString(q_str.utf16());//remember use SysFreeString on BSTR //QString<->LPCSTR QString::toLocal8Bit().constData(); QString::fromLocal8Bit ( const char * str, int size = -1 ); //QString<->LPCWSTR QString::utf16(); QString::fromUtf16 ( const ushort * unicode, int size = -1 ); //QString<->CString CString c_str(qstring::utf16()); QString fromUtf16 (LPCTSTR(c_str) ); CString转换为char* //1.传给未分配内存的const char* (LPCTSTR)指针. CString cstr(asdd); const char* ch = (LPCTSTR)cstr;//ch指向的地址和cstr相同。但由于使用const保证ch不会修改,所以安全. //2.传给未分配内存的指针. CString cstr = "ASDDSD"; char *ch = cstr.GetBuffer(cstr1.GetLength() + 1); cstr.ReleaseBuffer(); //修改ch指向的值等于修改cstr里面的值. //PS:用完ch后,不用delete ch,因为这样会破坏cstr内部空间,容易造成程序崩溃. //3.第二种用法。把CString 值赋给已分配内存的char *。 CString cstr1 = "ASDDSD"; int strLength = cstr1.GetLength() + 1; char *pValue = new char[strLength]; strncpy(pValue, cstr1, strLength); //4.第三种用法.把CString 值赋给已分配内存char[]数组. CString cstr2 = "ASDDSD"; int strLength1 = cstr1.GetLength() + 1; char chArray[100]; memset(chArray,0, sizeof(bool) * 100); //将数组的垃圾内容清空. strncpy(chArray, cstr1, strLength1); //5.如果上述都不行,使用以下方法 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; //成功输出字符串"Hello,World" 从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); }
时间: 2024-11-07 06:46:37