QString與char *的相互轉換

來源:互聯網
上載者:User

 本文轉自:http://blog.csdn.net/lanmanck/archive/2009/07/05/4322656.aspx

如何將QString轉換為char *或者相反

 註:如下方法應該是qt3.0的,一些方法大同小異,請參考qt4的類文檔說明。

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.toLocal8Bit()
例如
printf("%s", (const char*) str.toLocal8Bit());

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);
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.