AtUtf8.h

  1. #ifndef _QT_UTF8_H
  2. #define _QT_UTF8_H
  3. #include <QString>
  4. #include <string>
  5. using std::string;
  6. class AfUtf8
  7. {
  8. public:
  9. // QString(Unicode) -> std::string (UTF8)
  10. static string ToString(const QString& qstr)
  11. {
  12. QByteArray arr = qstr.toUtf8();
  13. string cstr = arr.data();
  14. return cstr;
  15. }
  16. // std::string (UTF8) -> QString(Unicode)
  17. static QString ToQString(const string& cstr)
  18. {
  19. QString qstr = QString::fromUtf8(cstr.c_str(), cstr.length());
  20. return qstr;
  21. }
  22. };
  23. #endif

AtGbk.h

  1. #ifndef _QT_GBK_H
  2. #define _QT_GBK_H
  3. #include <QString>
  4. #include <QTextCodec>
  5. #include <string>
  6. using std::string;
  7. class AfGbk
  8. {
  9. public:
  10. // QString(Unicode) -> std::string (GBK)
  11. static string ToString(const QString& qstr)
  12. {
  13. QTextCodec* pCodec = QTextCodec::codecForName("gb2312");
  14. if(!pCodec) return "";
  15. QByteArray arr = pCodec->fromUnicode(qstr);
  16. string cstr = arr.data();
  17. return cstr;
  18. }
  19. // std::string (GBK) -> QString(Unicode)
  20. static QString ToQString(const string& cstr)
  21. {
  22. QTextCodec* pCodec = QTextCodec::codecForName("gb2312");
  23. if(!pCodec) return "";
  24. QString qstr = pCodec->toUnicode(cstr.c_str(), cstr.length());
  25. return qstr;
  26. }
  27. };
  28. #endif

http://blog.csdn.net/bladeandmaster88/article/details/53469959

string与QString转换(string既可以是utf8,也可以是gbk)的更多相关文章

  1. QString 与 string转换

    [1]QString 转换为string QString qString("好好学习天天向上"); std::string stdString = qString.toStdStr ...

  2. QString与string的相互转换

    1.QString转换String string s = qstr.toStdString(); 2.String转换QString QString qstr2 = QString::fromStdS ...

  3. QString与string的相互转换【转载】

    文章转载自https://blog.csdn.net/qq_33485434/article/details/80680506 1.QString转换String string s = qstr.to ...

  4. JAVA中List转换String,String转换List,Map转换String,String转换Map之间的转换类

    <pre name="code" class="java"></pre><pre name="code" cl ...

  5. android 中List转换String,String转换List 改进版本

    原来博客地址http://blog.csdn.net/qq7342272/article/details/6830907 使用原作者贴的代码不是很好用,不能正常运行,所以我稍微改进了一下,特来分享给大 ...

  6. C#中char[]与string之间的转换;byte[]与string之间的转化

    目录 1.char[]与string之间的转换 2.byte[]与string之间的转化 1.char[]与string之间的转换 //string 转换成 Char[] string str=&qu ...

  7. string与QString之间的转换(两种方法:fromStdString直接转换,或者fromLocal8Bit(cstr.c_str())

    string str;QString qstr; //从QString 到 std::stringstr = qstr.toStdString(); //从std::string 到QStringqs ...

  8. Qt QString与string的转换

    QString --> string QString.toStdString();   string --> QString QString::fromStdString(string);

  9. QString, Std::string, char *相互转换

    Qt 库中对字符串类型进行了封装,QString 类提供了所有字符串操作方法,给开发带来了便利. 由于第三方库的类型基本上都是标准的类型,即使用std::string或char *来表示字符 (串) ...

随机推荐

  1. 【71.76%】【codeforces 732A】Buy a Shovel

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  2. Function函数

    一般大家都用这个写法来定义一个函数: function Name([parameters]){ functionBody }; //alert(typeof Name) // Function 当我们 ...

  3. WPF 使用 Edge 浏览器

    原文:WPF 使用 Edge 浏览器 版权声明:博客已迁移到 http://lindexi.gitee.io 欢迎访问.如果当前博客图片看不到,请到 http://lindexi.gitee.io 访 ...

  4. Linux开关命令(shutdown,reboot,halt,init)

    命令简短 shutdown,poweroff,reboot,halt,init都能够进行关机,大致使用方法. /sbin/halt     [-n] [-w] [-d] [-f] [-i] [-p] ...

  5. exponential family distribution(指数族分布)

    1. exponential family 给定参数 η,关于 x 的指数族分布定义为如下的形式: p(x∣∣η)=h(x)g(η)exp{ηTu(x)} 其中 x 可以为标量也可以为矢量,可以为离散 ...

  6. 解决关于archlinux升级至3.16.1,Xorg桌面环境无法进入的问题

    [现象]有终端输入username,password后,桌面环境启动失败. [报错]log文件里显示: (EE) Fatal server error: (EE) xf86OpenConsole: V ...

  7. 对XAML进行编辑的辅助类(XamlHelper)

    原文:对XAML进行编辑的辅助类(XamlHelper) // XamlHelper.cs// --------------------------------------------// 对XAML ...

  8. #747 –在WPF程序的触摸操作中使用惯性移动 (Implementing Inertia during Touch Manipulation)

    原文:#747 –在WPF程序的触摸操作中使用惯性移动 (Implementing Inertia during Touch Manipulation) 原文地址:https://wpf.2000th ...

  9. WPF 呼吸灯特效

    原文:WPF 呼吸灯特效 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u014117094/article/details/46738621 pa ...

  10. cross-compile-openssl-windows.sh,cross-compile-curl-windows.sh,cross-compile-zlib-windows.sh,build-zlib-visual-studio-2015-cli.bat

    https://gist.github.com/artynet build zlib with Visual Studio CLI toolhttps://gist.github.com/artyne ...