先看看官方是如何说的:
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);
方法二 ----------------------------------------- 如果是中文系统
直接用 (const char*) str.local8Bit() 例如 printf("%s", (const char*) str.local8Bit());
str是一个QString
方法三 ----------------------------------------- char str[64]; QTextCodec *textcod = QTextCodec::codecForName("GBK"); QCString string1 = textcod ->fromUnicode(listbox1->currentText()); strcpy(str,string1);
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后加上这句:
QUOTE:
QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB18030"));
然后是从UTF8编码到GB编码的字符串转换方法:
QUOTE:
QString Utf8_To_GB(QString strText) { return QString::fromUtf8(strText.toLocal8Bit().data()); }
至于从GB到UTF8,那大家就经常用了:
QUOTE:
QString GB_To_Utf8(char *strText) { return QString::fromLocal8Bit(strText); }
剖析Qt 各种数据类型转换
Qt 各种数据类型转换是本文介绍的内容,对于类型不同的转换,本人认为还是很好实现的。先来看内容。
AD:
本文介绍的是Qt 各种数据类型转换,病没有多少内容,只是实例操作了一下,多多参考!如果你是初学者的话。在编程的过程中,总是不断的去转换数据类型。先来看看内容。
1、QString --> string
QString.toStdString();
2、string --> QString
QString::fromStdString(string)
3、QString --->int,double,char *
QString::toInt()
QString::toDouble()
QString.toStdString().c_str();
QString.toLocal8Bit().data(); 这个要好
4、int double char* --->string
可以采用<sstream>里的stringstream
以int 为例,int a = 3;
stringstream ss;
string strInt;
ss<<a;
ss>>strInt;
其他两个一样。
5、int double char*装QString
一种方法可以先转string,再转QString。另一种方法可以查看QString类的函数。
QString::number()这个静态函数,参数可以是int,也可以是double。
6、double int的互转
可采用static_cast
7、int->char*
char a[6];
sprintf(a,"A.%d",i++)
输出形势为:A.1 A.2 A.3
小结:关于剖析Qt 各种数据类型转换的内容介绍完了,希望本文对有所帮助!
|