QString CString char三者之转换集锦(转)
(2011-10-26 21:36:38)
标签:
qt |
分类: QT |
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_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) )
传给未分配内存的const char* (LPCTSTR)指针.
3.第二种用法。把CString 值赋给已分配内存的char *。
4.第三种用法.把CString 值赋给已分配内存char[]数组.
如果上述都不行:
CString转换为char*
成功输出字符串"Hello,World"
原因:
原来在VC++
2005以前,应用程序默认都是关闭对Unicode的支持的,而在VC2005中,默认打开了对它的支持,CString对应的字符串应该是TCHAR,TCHAR的定义是这样的,
所以在工程中应该可以关闭对于Unicode的支持,从而可以直接转换。这个做法是右击工程名—〉Property—〉General中的character
set中选择notset,这样,本文开头的那段代码就可以正确的执行了。
QString GB_To_Utf8(char *strText)
{
}
如何将QString转换为char *或者相反
How can I convert a QString to char* and vice versa ?(trolltech)
Answer:
In order to convert a QString to a char*, then you first need to
get a latin1 representation of the string by calling toLatin1() on
it which will return a QByteArray. Then call data() on the
QByteArray to get a pointer to the data stored in the byte array.
See the documentation:
See the following example for a demonstration:
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QString str1 = "Test";
QByteArray ba = str1.toLatin1();
const char *c_str2 = ba.data();
printf("str2: %s", c_str2);
return app.exec();
}
Note that it is necessary to store the bytearray before you call
data() on it, a call like the following
const char *c_str2 = str2.toLatin1().data();
will make the application crash as the QByteArray has not been stored and hence no longer exists.
To convert a char* to a QString you can use the QString constructor
that takes a QLatin1String, e.g:
QString string = QString(QLatin1String(c_str2)) ;
还有其他多种方法:
方法一 -----------------------------------------
#define G2U(s) (
QTextCodec::codecForName("GBK")->toUnicode(s)
)
#define U2G(s) (
QTextCodec::codecForName("GBK")->fromUnicode(s)
)
QString str;
QCString cstr;
str = G2U("中文输入");
cstr = U2G(str);
QCString有这样一个重载运算符
operator const char * () const
可以这样
printf("%s\n", (const char*) cstr);
或是copy出来
char buf[1024];
strcpy(buf, (const char*) cstr);
方法二 -----------------------------------------
如果是中文系统
直接用
例如
printf("%s", (const char*) str.local8Bit());
str是一个QString
方法三 -----------------------------------------
char str[64];
QTextCodec *textcod = QTextCodec::codecForName("GBK");
QString和Std::string
从char*到 QString可以从fromLocal8Bit()转化
std::string有c_str()的函数使再转化为char*
QString有toAscii()记不清了
你可以看看.
又是我的粗心酿成大错,我重新查看了一下Qt文档,原来Qt可以直接从std::wstring产生一个QString,用
QString::fromStdWString(const std::wstring
&)这个静态成员函数即可。我试了试用std::string的c_str()返回的char
*构造的QString不能再保存原先的中文信息,而用std::wstring构造的QString则可以用qDebug()输出原先的中文信息
GB编码与UTF8编码的转换
在主函数app后加上这句:
QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB18030"));
然后是从UTF8编码到GB编码的字符串转换方法:
QString Utf8_To_GB(QString strText)
{
}
至于从GB到UTF8,那大家就经常用了:

加载中…