QT4.6中文GB2312与utf-8和unicode及URL encoding %编码汇总
2015-12-17 15:44
645 查看
QT4.6中文GB2312与utf-8和unicode及URL encoding %编码汇总
说明: 转载windows中ie地址栏写“我们”发送到服务器端会变为“%CE%D2%C3%C7”,是GB2312转换成URL 百分号编码的形式
ubuntu中firefox地址栏写“我们”发送到服务器变成"%E6%88%91%E4%BB%AC",是UTF-8转换成URL 百分号编码的形式
下面分享汇总后的转换代码:(我的环境:ubuntu9.10 QT4.6)
//Ubuntu firefox http get "我们" %E6%88%91%E4%BB%AC UTF-8
//Windows ie http get "我们" %CE%D2%C3%C7 GB2312
char* s="0x我们"; //UTF-8 char*
QString strT1="0x%CE%D2%C3%C7"; //GB2312的URL百分号编码
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())
{
ByteArrayOut.append(strInput.mid(i+1,2).toShort(0,16));
i=i+3;
}
else
{
ByteArrayOut.append(strInput.mid(i,1));
i++;
}
}
else
{
ByteArrayOut.append(strInput.mid(i,1));
i++;
}
}//For end
}// PercentEncoding2ByteArray end
相关文章推荐
- QT学习之类型转换,char*,QString,int,QByteArray之间的转换。
- QT实现图片的缩放
- QT封装qlabel可以实现画图,画图拖动
- QT下显示中文
- Windows下Qt Creator使用版本控制器Git来进行版本控制,GitHub进行代码托管
- Visual Studio 2015编译安装配置QT5.5.1(含QTWEBKIT)
- QT之QCustomPlot(二)
- OpenCV2.4.9+Qt5.4 MinGW Win7 源码编译
- Qt4.8.5下 Qtserialport 安装中找不到cfgmgr32.h的解决
- QT配置问题
- QT编译子目录项目出现sub-xx-make-first-ordered error5 解决方法
- QT 设置表中某一行的颜色遇到问题
- 转:pyqt实例
- Qt界面设计常用的一些代码(即Qt style Sheets)
- Qt 官方一键动态发布技能
- DLL在Qt中的创建与使用
- QT之QCustomPlot(一)
- QT5.5.0+VS2013(X64)+hidapi的USB继续深究之 QT的汉字显示
- Qt书籍推荐
- 上位机界面:pyqt