zoukankan      html  css  js  c++  java
  • Qt实现16进制unicode转utf-8以及国际音标编码问题

    由于项目需要,需要对网络资源进行解码。遇到编码问题。研究了下基本编码原理。于是有了下面两个通用代码

    [cpp] view plaincopy
     
    1. 1. 16进制unicode转换为utf-8中文显示  
    2.   
    3. QString unicodeToUtf_8(const QString &resStr)  
    4. {  
    5.     //  unicode转utf-8  思路: 一个unicode编码占用2字节。所以只需要用4个16进制数字结合表示就行了  
    6.   
    7.     QString temp;  
    8.     for (int i = 0; i< resStr.length();)  
    9.     {  
    10.         if (resStr.at(i) == '\')  
    11.         {  
    12.             QString str = resStr.mid(i+2, 4);  
    13.             temp.append(str.toUShort(0, 16));  
    14.             i += 6;  
    15.         }  
    16.         else  
    17.         {  
    18.             temp.append(resStr.at(i));  
    19.             ++i;  
    20.         }  
    21.     }  
    22.     QTextCodec *codec = QTextCodec::codecForName("UTF-8");  
    23.     QString desStr = codec->fromUnicode(temp);  
    24.     return QObject::trUtf8(desStr.toLatin1().data());  
    25. }  
    26.   
    27. 2. 如何显示网络中返回的16进制unicode编码的音标??  
    28.   
    29. QString getTheDecodeSymbol(const QString &resStr)  
    30. {  
    31.     // 如何把16进制的unicode编码音标显示在窗口中?  
    32.     // 比如: u00a0[u026anu02c8tau026at(u0259)lmu0259nt]  
    33.     // 解码思路如下: 先把16进制unicode编码转换为 10进制unicode编码  
    34.     // 转换方法就是下面的代码  
    35.     // 那么如何显示呢?  
    36.     // 借助QLabel的设置字体  
    37.     // tr("<span style='font-family:"MS Mincho";'>%1</span>").arg(data);即可  
    38.     QString desStr;  
    39.     for (int i = 0; i< resStr.length();)  
    40.     {  
    41.         if (resStr.at(i) == '\')  
    42.         {  
    43.             QString str = resStr.mid(i+2, 4);  
    44.             desStr.append(tr("&#%1;").arg(str.toInt(0, 16)));  
    45.             i += 6;  
    46.         }  
    47.         else  
    48.         {  
    49.             desStr.append(resStr.at(i));  
    50.             ++i;  
    51.         }  
    52.     }  
    53.     return desStr;  
    54. }  

    http://blog.csdn.net/wu5151/article/details/47107051

  • 相关阅读:
    阿里MaxCompute(原ODPS)如何修改列字段数据类型
    解决SQL语句在Dapper执行超时比Query慢的问题
    IIS部署FLASK网站
    如何在vscode中调试python scrapy爬虫
    [转]阿拉伯数字转一二三和壹贰叁
    第三册复习
    不知道的口语
    跟小丸子学基础口语21-25
    跟小丸子学基础口语16-20
    跟小丸子学基础口语11-15
  • 原文地址:https://www.cnblogs.com/findumars/p/5079453.html
Copyright © 2011-2022 走看看