qt 字符串转换 最最详细版本

来源:互联网 发布:程序员手机壁纸 编辑:程序博客网 时间:2024/04/30 22:06

这篇文章是在Blogspot上看到的一篇文章,能够解决QString, wchar_t *, TCHAR和其他字符或字符串类型之间的转换,方便在使用Windows API的时候转换的麻烦。

原文地址:http://tkrotoff.blogspot.com/2010/04/code-snippets-about-qstring-wchart.html

1//QString to wchar_t *:
2const wchar_t * encodedName = reinterpret_cast<const wchar_t *>(fileName.utf16());
3 
4//QString to char * given a file name:
5QByteArray fileName = QFile::encodeName(aFileName);
6const char * encodedName = fileName.constData(); //Valid as long as fileName exists
7 
8//QString to char * (general case):
9const char * tmp = str.toUtf8().constData();

Windows 数据类型: http://msdn.microsoft.com/en-us/library/aa383751.aspx

01//TCHAR:
02#ifdef UNICODE
03    typedef wchar_t TCHAR;
04#else
05    typedef char TCHAR;
06#endif
07 
08//LPCTSTR:
09#ifdef UNICODE
10    typedef LPCWSTR LPCTSTR;
11#else
12    typedef LPCSTR LPCTSTR;
13#endif
14 
15//LPCSTR:
16typedef const char * LPCSTR;
17 
18//LPCWSTR:
19typedef const wchar_t * LPCWSTR;
20 
21//LPCWSTR to QString:
22QString 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规范。

01QString::fromAscii ( const char * str, int size = -1 );
02QString::fromLatin1 ( const char * str, int size = -1 );
03QString::fromLocal8Bit ( const char * str, int size = -1 );
04QString::fromRawData ( const QChar * unicode, int size );
05QString::fromStdString ( const std::string & str );
06QString::fromStdWString ( const std::wstring & str );
07QString::fromUcs4 ( const uint * unicode, int size = -1 );
08QString::fromUtf8 ( const char * str, int size = -1 );
09QString::fromUtf16 ( const ushort * unicode, int size = -1 );
10QString::fromWCharArray ( const wchar_t * string, int size = -1 );
11 
12//qstring ->std::string
13QString::toStdString () ;
14QString::toStdWString ();
15 
16//BSTR<->QString,不太了解BSTR是什么,还没用到过,所以不知道对不对
17BSTR bstr_str;
18QString q_str((QChar*)bstr_str, wcslen(bstr_str));
19bstr_str = SysAllocString(q_str.utf16());//remember use SysFreeString on BSTR
20 
21//QString<->LPCSTR
22QString::toLocal8Bit().constData();
23QString::fromLocal8Bit ( const char * str, int size = -1 );
24 
25//QString<->LPCWSTR
26QString::utf16();
27QString::fromUtf16 ( const ushort * unicode, int size = -1 );
28 
29//QString<->CString
30CString c_str(qstring::utf16());
31QString fromUtf16 (LPCTSTR(c_str) );

CString转换为char*

01//1.传给未分配内存的const char* (LPCTSTR)指针.
02CString cstr(asdd);
03const char* ch = (LPCTSTR)cstr;//ch指向的地址和cstr相同。但由于使用const保证ch不会修改,所以安全.
04 
05//2.传给未分配内存的指针.
06CString cstr = "ASDDSD";
07char *ch = cstr.GetBuffer(cstr1.GetLength() + 1);
08cstr.ReleaseBuffer();
09//修改ch指向的值等于修改cstr里面的值.
10//PS:用完ch后,不用delete ch,因为这样会破坏cstr内部空间,容易造成程序崩溃.
11 
12//3.第二种用法。把CString 值赋给已分配内存的char *。
13CString cstr1 = "ASDDSD";
14int strLength = cstr1.GetLength() + 1;
15char *pValue = new char[strLength];
16strncpy(pValue, cstr1, strLength);
17 
18//4.第三种用法.把CString 值赋给已分配内存char[]数组.
19CString cstr2 = "ASDDSD";
20int strLength1 = cstr1.GetLength() + 1;
21char chArray[100];
22memset(chArray,0, sizeof(bool) * 100); //将数组的垃圾内容清空.
23strncpy(chArray, cstr1, strLength1);
24 
25//5.如果上述都不行,使用以下方法
26CString origCString("Hello, World!");
27wchar_t* wCharString = origCString.GetBuffer(origCString.GetLength()+1);
28size_t origsize = wcslen(wCharString) + 1;
29size_t convertedChars = 0;
30char *CharString;
31CharString=new char(origsize);
32wcstombs_s(&convertedChars, CharString, origsize, wCharString , _TRUNCATE);
33cout << CharString << endl; //成功输出字符串"Hello,World"

从UTF8编码到GB编码的字符串转换方法:

1QString Utf8_To_GB(QString strText)
2{
3    return QString::fromUtf8(strText.toLocal8Bit().data());
4}

从GB编码到UTF8编码的字符串转换方法

查看源代码
打印帮助
1QString GB_To_Utf8(char *strText)
2{
3    return QString::fromLocal8Bit(strText);
4}