首页 > 代码库 > GBK转换封装

GBK转换封装

//    封装 #ifndef _QT_GBK_H#define _QT_GBK_H#include<QString>#include<QTextCodec>#include<string>using std::string;class GBK{public:    static string FromUnicode(const QString& qstr)    {        QTextCodec* pCodec = QTextCodec::codecForName("gb2312");        if (!pCodec)return "";        QByteArray arr = pCodec->fromUnicode(qstr);        string cstr=arr.data();        return cstr;    }    static QString ToUnicode(const string& cstr)    //QBT转换QString    {        QTextCodec* pCodec = QTextCodec::codecForName("gb2312");        if (!pCodec)return "";        QString qstr = pCodec->toUnicode(cstr.c_str(), cstr.length());        return qstr;    }};#endif//使用::    QString str = GBK::ToUnicode("你好中国");    ui.linetxt->setText(str);    //uilinetxt->setText(GBK::ToUnicode("你好中国"))        string text = GBK::FromUnicode(ui.linetxt->text());//获取用户输入

 

GBK转换封装