一些QT的字元轉換

來源:互聯網
上載者:User

說明:windows中ie地址欄寫“我們”

發送到伺服器端會變為“%CE%D2%C3%C7”,

是GB2312轉換成URL 百分比符號編碼的形式ubuntu中firefox地址欄寫“我們”發送到伺服器變成”%E6%88%91%E4%BB%AC”,

是UTF-8轉換成URL 百分比符號編碼的形式

下面分享匯總後的轉碼:(我的環境:ubuntu10.10 QT4.6.2)01

//Ubuntu firefox http get "我們" %E6%88%91%E4%BB%AC UTF-802

//Windows ie http get "我們" %CE%D2%C3%C7 GB231203

char* s="0x我們"; 

 //UTF-8 char*

QString strT1="0x%CE%D2%C3%C7"; 

 //GB2312的URL百分比符號編碼05

QByteArray baT1,baT2,baT3,baT4;

PercentEncoding2ByteArray(strT1, baT1); //自己寫的函數,由於QUrl::fromPercentEncoding僅適用UTF-8的URL編碼

strT1=QTextCodec::codecForName("GB2312")->toUnicode(baT1); //GB2312's QByteArray Convert to QString Unicode

baT2=QTextCodec::codecForName("GB2312")->fromUnicode(strT1); //QString Unicode convert to GB2312 QByteArray

QString strT2,strT3,strT4,strT5;

strT2=QTextCodec::codecForName("UTF-8")->toUnicode("0x我們"); //UTF-8 Char* to Unicode QString

baT3=QTextCodec::codecForName("UTF-8")->fromUnicode(strT2); //Unicode QString to UTF-8 QByteArray

strT3=QTextCodec::codecForName("UTF-8")->toUnicode(s); //UTF-8 Char* to Unicode QString

strT4=QUrl::toPercentEncoding(QString::fromLocal8Bit(s)); //UTF-8 Char* to PercentEncoding (UTF-8 format)

strT5=QUrl::fromPercentEncoding("0x%E6%88%91%E4%BB%AC"); //PercentEncoding (UTF-8 format) to Unicode

baT4=QTextCodec::codecForName("GB2312")->fromUnicode(QTextCodec::codecForName("UTF-8")->toUnicode(s)); //UTF-8 Char* to GB2312 QByteArray

另附一個小函數:(用於GB2312的URL百分比符號編碼轉換)

void PercentEncoding2ByteArray(QString strInput, QByteArray & ByteArrayOut)

{

 for(int i=0; i<strInput.length();)

 {

 if (0==QString::compare(strInput.mid(i,1), QString("%")))

 {

 if ((i+2)<strInput.length())

 {09 ByteArrayOut.append(strInput.mid(i+1,2).toShort(0,16));

 i=i+3;11 }12 else13 {14 ByteArrayOut.append(strInput.mid(i,1));

 i++;

 }

 }

 else

 {

 ByteArrayOut.append(strInput.mid(i,1));

 i++;

 }

 }

//For end

}// PercentEncodin

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.