zoukankan      html  css  js  c++  java
  • Qt中文编码和QString类Unicode编码转换

     
    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
    本文链接:https://blog.csdn.net/g423tgl234/article/details/52222569
    1 window中文GBK编码和Unicode编码转换

    //GBK‐> QString
    QString str = QString::fromLocal8Bit("新浪微博");
     
    //QString ‐> GBK
    QString text = ui.lineEdit‐>text();
    QByteArray bytes = text.toLocal8Bit();
    const char* gbk = bytes.data();
    2 Linux中文中文编码和Unicode编码转换

    UTF‐8‐> QString :
    char utf8[] = {0xE4 ,0xBD, 0xA0, 0xE5, 0xA5, 0xBD, 0};  
    QString str2 = QString::fromUtf8(utf8);
     
    QString ‐> UTF‐8:
    QString text = ui.lineEdit‐>text();
    QByteArray bytes = text.toUtf8();
    const char* utf8 = bytes.data();
    int size = bytes.size();
     
    3 使用QTextCodec类实现编码<pre name="code" class="cpp">// QString(Unicode) ‐> std::string (GBK)
    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;
    }
    // std::string (GBK) ‐> QString(Unicode)
    static QString ToUnicode(const string& cstr)
    {
    QTextCodec* pCodec = QTextCodec::codecForName("gb2312");
    if(!pCodec) return "";
    QString qstr = pCodec‐>toUnicode(cstr.c_str(), cstr.length());
    return qstr;
    }
  • 相关阅读:
    ksframework的xlua版本
    unity摄像机脚本
    代码重构:用工厂+策略模式优化过多的if else代码块
    在Unity中创建攻击Slot系统
    Unity运用GPU代替CPU处理和计算简单测试
    程序员工具集
    Unity开发-你必须知道的优化建议
    unity向量计算
    ClassFoo-IT知识花园
    BZOJ 3446: [Usaco2014 Feb]Cow Decathlon( 状压dp )
  • 原文地址:https://www.cnblogs.com/lvdongjie/p/11821296.html
Copyright © 2011-2022 走看看