这篇文章是在Blogspot上看到的一篇文章,能够解决QString, wchar_t *, TCHAR和其他字符或字符串类型之间的转换,方便在使用Windows API的时候转换的麻烦。
原文地址:http://tkrotoff.blogspot.com/2010/04/code-snippets-about-qstring-wchart.html
http://blog.csdn.net/hufeng825/article/details/5776175
2 | const wchar_t * encodedName = reinterpret_cast < const wchar_t *>(fileName.utf16()); |
5 | QByteArray fileName = QFile::encodeName(aFileName); |
6 | const char * encodedName = fileName.constData(); |
9 | const char * tmp = str.toUtf8().constData(); |
Windows 数据类型: http://msdn.microsoft.com/en-us/library/aa383751.aspx
03 | typedef wchar_t TCHAR ; |
10 | typedef LPCWSTR LPCTSTR ; |
12 | typedef LPCSTR LPCTSTR ; |
16 | typedef const char * LPCSTR ; |
19 | typedef const wchar_t * LPCWSTR ; |
22 | 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规范。
01 | QString::fromAscii ( const char * str, int size = -1 ); |
02 | QString::fromLatin1 ( const char * str, int size = -1 ); |
03 | QString::fromLocal8Bit ( const char * str, int size = -1 ); |
04 | QString::fromRawData ( const QChar * unicode, int size ); |
05 | QString::fromStdString ( const std::string & str ); |
06 | QString::fromStdWString ( const std::wstring & str ); |
07 | QString::fromUcs4 ( const uint * unicode, int size = -1 ); |
08 | QString::fromUtf8 ( const char * str, int size = -1 ); |
09 | QString::fromUtf16 ( const ushort * unicode, int size = -1 ); |
10 | QString::fromWCharArray ( const wchar_t * string, int size = -1 ); |
13 | QString::toStdString () ; |
14 | QString::toStdWString (); |
18 | QString q_str((QChar*)bstr_str, wcslen(bstr_str)); |
19 | bstr_str = SysAllocString(q_str.utf16()); |
22 | QString::toLocal8Bit().constData(); |
23 | QString::fromLocal8Bit ( const char * str, int size = -1 ); |
27 | QString::fromUtf16 ( const ushort * unicode, int size = -1 ); |
30 | CString c_str(qstring::utf16()); |
31 | QString fromUtf16 ( LPCTSTR (c_str) ); |
CString转换为char*
03 | const char * ch = ( LPCTSTR )cstr; |
06 | CString cstr = "ASDDSD" ; |
07 | char *ch = cstr.GetBuffer(cstr1.GetLength() + 1); |
13 | CString cstr1 = "ASDDSD" ; |
14 | int strLength = cstr1.GetLength() + 1; |
15 | char *pValue = new char [strLength]; |
16 | strncpy (pValue, cstr1, strLength); |
19 | CString cstr2 = "ASDDSD" ; |
20 | int strLength1 = cstr1.GetLength() + 1; |
22 | memset (chArray,0, sizeof ( bool ) * 100); |
23 | strncpy (chArray, cstr1, strLength1); |
26 | CString origCString( "Hello, World!" ); |
27 | wchar_t * wCharString = origCString.GetBuffer(origCString.GetLength()+1); |
28 | size_t origsize = wcslen(wCharString) + 1; |
29 | size_t convertedChars = 0; |
31 | CharString= new char (origsize); |
32 | wcstombs_s(&convertedChars, CharString, origsize, wCharString , _TRUNCATE); |
33 | cout << CharString << endl; |
从UTF8编码到GB编码的字符串转换方法:
1 | QString Utf8_To_GB(QString strText) |
3 | return QString::fromUtf8(strText.toLocal8Bit().data()); |
从GB编码到UTF8编码的字符串转换方法
1 | QString GB_To_Utf8( char *strText) |
3 | return QString::fromLocal8Bit(strText); |