AtUtf8.h
[cpp] view plain copy
#ifndef _QT_UTF8_H
#define _QT_UTF8_H
#include <QString>
#include <string>
using std::string;
class AfUtf8
{
public:
// QString(Unicode) -> std::string (UTF8)
static string ToString(const QString& qstr)
{
QByteArray arr = qstr.toUtf8();
string cstr = arr.data();
return cstr;
}
// std::string (UTF8) -> QString(Unicode)
static QString ToQString(const string& cstr)
{
QString qstr = QString::fromUtf8(cstr.c_str(), cstr.length());
return qstr;
}
};
#endif
AtGbk.h
[cpp] view plain copy
#ifndef _QT_GBK_H
#define _QT_GBK_H
#include <QString>
#include <QTextCodec>
#include <string>
using std::string;
class AfGbk
{
public:
// QString(Unicode) -> std::string (GBK)
static string ToString(const QString& qstr)
{
QTextCodec* pCodec = QTextCodec::codecForName(“gb2312”);
if(!pCodec) return “”;
QByteArray arr = pCodec->fromUnicode(qstr);
string cstr = arr.data();
return cstr;
}
// std::string (GBK) -> QString(Unicode)
static QString ToQString(const string& cstr)
{
QTextCodec* pCodec = QTextCodec::codecForName(“gb2312”);
if(!pCodec) return “”;
QString qstr = pCodec->toUnicode(cstr.c_str(), cstr.length());
return qstr;
}
};
#endif
http://blog.csdn.net/bladeandmaster88/article/details/53469959
最新评论