为了让包含中文的 URL 可以使用,需要进行 UrlEncode 编码。Java 中有现成的类库可以使用,其实我们 Qt 进行网络编程时,框架中也带了 UrlEncode 的功能。
下面是我写的一个小程序,带图形界面的,可以对包含中文的 URL 进行 UrlEncode 。
头文件:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QLineEdit>
#include <QLabel>
#include <QPushButton>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
public slots:
void onConvert();
protected:
void resizeEvent(QResizeEvent *);
private:
QLabel *m_labelInputPrompt;
QLineEdit *m_editInput;
QLabel *m_labelOutputPrompt;
QLabel *m_labelOutput;
QPushButton * m_btnConvert;
};
#endif // WIDGET_H
源文件:
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QHBoxLayout * layoutInput = new QHBoxLayout();
m_labelInputPrompt = new QLabel(QString::fromLocal8Bit("请输入汉字:"));
m_editInput = new QLineEdit();
m_btnConvert = new QPushButton(QString::fromLocal8Bit("转换"));
layoutInput->addWidget(m_labelInputPrompt, 1);
layoutInput->addWidget(m_editInput, 2);
layoutInput->addWidget(m_btnConvert);
QHBoxLayout * layoutOutput = new QHBoxLayout();
m_labelOutputPrompt = new QLabel(QString::fromLocal8Bit("GB2312 Percent Encode:"));
m_labelOutput = new QLabel();
m_labelOutput->setFrameShape(QFrame::Box);
layoutOutput->addWidget(m_labelOutputPrompt);
layoutOutput->addWidget(m_labelOutput);
QVBoxLayout * layout = new QVBoxLayout;
layout->addLayout(layoutInput);
layout->addLayout(layoutOutput);
setLayout(layout);
connect(m_btnConvert, SIGNAL(clicked()), this, SLOT(onConvert()));
}
Widget::~Widget()
{
}
void Widget::resizeEvent(QResizeEvent *)
{
}
void Widget::onConvert()
{
QString strInput = m_editInput->text();
if(strInput.isEmpty())
{
return;
}
QTextCodec * codecGB2312 = QTextCodec::codecForName("GB2312");
QByteArray byteArrayGB2312 = codecGB2312->fromUnicode(strInput);
QByteArray byteArrayPercentEncoded = byteArrayGB2312.toPercentEncoding();
m_labelOutput->setText(QString(byteArrayPercentEncoded));
}
上面的代码,核心的就是 onConvert() 函数,其它的都是搭建界面需要的。
下面是 main() 函数:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//set codecs to UTF-8
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
QFont font("Microsoft yahei");
font.setPixelSize(28);
a.setFont(font);
Widget w;
w.show();
return a.exec();
}
main() 函数很简单,启动界面而已。
在 onConvert() 函数中,我们从编辑框获取文本,构建一个 GB2312 编码的 TextCodec ,然后使用其 fromUnicode() 方法将得到的文本转化为使用 GB2312 编码过的 QByteArray ,再使用 QByteArray 的 toPercentEncoding() 方法转换成正常的 URL 格式。
对于包含汉字的 URL ,汉字的编码常用的有两种 GB2312 和 UTF-8 ,本文是用采用 GB2312 编码进行转换。
如果是 UTF-8 ,更简单,在上面的程序上稍作修改,不用构造 GB2312 的 TextCodec ,直接使用从编辑框获取的 QString 对象的 toUtf8() 方法得到 QByteArray,然后调用 toPercentEncoding() 方法即可。 onConvert() 代码如下:
QString strInput = m_editInput->text();
if(strInput.isEmpty())
{
return;
}
QByteArray ba = strInput.toUtf8();
QByteArray byteArrayPercentEncoded = ba.toPercentEncoding();
m_labelOutput->setText(QString(byteArrayPercentEncoded));
至于 UrlDecode ,和 UrlEncode 过程是相反的,就不再细说了。
一个小插曲,我的小程序原本是用 Qt 4.x 写的,今天发现在 Qt 5.2.0 上,QTextCodec 的一些方法没有了,如 setCodecForTr / setCodecForCStrings 。
分享到:
相关推荐
urlEncode or urlDecode
自己写的,可能有bug,请大家一块学习 环境为PB12 函数(及参数) 作用 arraysort 对一维数组进行排序 decto 将十进制数字转成其它进制字符串 ...urlencode 将指定字符串以进行指定字符集url编码 涨价了,哈哈
易语言URL编解码源码,URL编解码,URLEncode,URLDecode
这个是 utf-8版本的 js实现 php的 urlencode() 和 urldecode()两个函数的功能。...在传送cookie的时候 在 php端实现 url编码 但要用 js来解码cookie的时候 就出现汉字不能不能识别的问题 这个 js 很好的解决
UrlEncodeUrlDecodeUrl解码Url编码工具 基于UrlEncode,UrlDecode的解码编码工具,支持双向。
基于UrlEncode,UrlDecode的解码编码工具,支持双向。
NULL 博文链接:https://lindows.iteye.com/blog/259125
今天小编就为大家分享一篇Python3中urlencode和urldecode的用法详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
包括(UrlEncode、UrlDecode、HtmlEncode、HtmlDecode),html文件,直接在浏览器操作,可查看源码
urlencode urldecode实现,在xcode里使用c++混编
代码如下:‘================================================ ‘函数名:URLDecode ‘作 用:URL解码 ‘================================================ Function URLDecode(ByVal urlcode) Dim start,final,...
idea插件toolsMix (记录下班时间、实时中英文翻译、jsonView、urlEncode、urlDecode、timeFormate) 解压toolsMix.zip,toolsMix目录下的zip包为打包好的插件包,可以直接安装
主要介绍了用JavaScript实现PHP的urlencode与urldecode函数,很多情况下我们用了出来php urlencode出来的网址,需要的朋友可以参考下
易语言url编解码汇编源码,url编解码汇编,URLEncode,URLDecode
主要介绍了php自定义urlencode,urldecode函数,实例分析了php字符串转码的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
虽然ASP中的request会自动解码经过url编码的字符串,但是Request.BinaryRead(Request.TotalBytes)取得post数据时却不会解码,所以要进行解码。 下面是我找到的一个ASP中server.urlencode函数的解码函数 代码如下: ...
JS版 UrlEncode 和 UrlDecode 函数