zoukankan      html  css  js  c++  java
  • string与QString转换(string既可以是utf8,也可以是gbk)

    AtUtf8.h

    [cpp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. #ifndef _QT_UTF8_H  
    2. #define _QT_UTF8_H  
    3.   
    4. #include <QString>  
    5. #include <string>  
    6. using std::string;  
    7.   
    8. class AfUtf8  
    9. {  
    10. public:  
    11.     // QString(Unicode) -> std::string (UTF8)  
    12.     static string ToString(const QString& qstr)  
    13.     {  
    14.         QByteArray arr = qstr.toUtf8();  
    15.         string cstr = arr.data();  
    16.         return cstr;  
    17.     }  
    18.   
    19.     // std::string (UTF8) -> QString(Unicode)  
    20.     static QString ToQString(const string& cstr)  
    21.     {  
    22.         QString qstr = QString::fromUtf8(cstr.c_str(), cstr.length());  
    23.         return qstr;  
    24.     }  
    25. };  
    26. #endif  

    AtGbk.h

    [cpp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. #ifndef _QT_GBK_H  
    2. #define _QT_GBK_H  
    3.   
    4. #include <QString>  
    5. #include <QTextCodec>  
    6. #include <string>  
    7. using std::string;  
    8.   
    9. class AfGbk  
    10. {  
    11. public:  
    12.     // QString(Unicode) -> std::string (GBK)  
    13.     static string ToString(const QString& qstr)  
    14.     {  
    15.         QTextCodec* pCodec = QTextCodec::codecForName("gb2312");  
    16.         if(!pCodec) return "";    
    17.   
    18.         QByteArray arr = pCodec->fromUnicode(qstr);  
    19.         string cstr = arr.data();  
    20.         return cstr;  
    21.     }  
    22.   
    23.     // std::string (GBK) -> QString(Unicode)  
    24.     static QString ToQString(const string& cstr)  
    25.     {  
    26.         QTextCodec* pCodec = QTextCodec::codecForName("gb2312");  
    27.         if(!pCodec) return "";  
    28.   
    29.         QString qstr = pCodec->toUnicode(cstr.c_str(), cstr.length());  
    30.         return qstr;  
    31.     }  
    32.   
    33. };  
    34. #endif  

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

  • 相关阅读:
    remote: You are not allowed to push code to this project
    Ubuntu 查看本机的ip
    git跟踪远程分支,查看本地分支追踪和远程分支的关系
    edgedb 基本试用
    influxdb 全家桶运行
    Introducing Outflux: a smart way out of InfluxDB
    使用outflux 导入influxdb 的数据到timescaledb
    edgedb 强大的对象关系数据库
    Announcing the Operate Preview Release: Monitoring and Managing Cross-Microservice Workflows
    goaccess iis w3c 自定义log 格式参考
  • 原文地址:https://www.cnblogs.com/findumars/p/6345115.html
Copyright © 2011-2022 走看看