Qt532_字符编码转换

1、测试代码:

    // http://blog.csdn.net/changsheng230/article/details/6588447
    QString str = QString::fromLocal8Bit("我是中国人");
    QString str2 = QString("本地文本");  // 乱码
    qDebug() << str;
    qDebug() << str2;

    // Method 2
    QTextCodec *codec = QTextCodec::codecForName("GBK"); // get the codec for KOI8-R
    //QString locallyEncoded = codec->toUnicode( "显示中文" );
    QString locallyEncoded = codec->toUnicode( "我是中国人" );
    qDebug() << locallyEncoded << endl;

    codec = QTextCodec::codecForName("gbk"); // get the codec for KOI8-R
    locallyEncoded = codec->toUnicode( "我是中国人" );
    qDebug() << locallyEncoded << endl;

    wchar_t *pwc = L"我是中国人";
    for (size_t i=0; i<wcslen(pwc); i++)
    {
        ushort us = pwc[i];
        qDebug() << "\t" << QString::number(us, 16).leftJustified(2, ‘0‘);
    }
    qDebug() << "";

    QChar *pcs = (QChar*)str.unicode();
    for (int i=0; i<str.length(); i++)
    {
        QChar c = pcs[i];
        ushort us = c.unicode();
        qDebug() << "\t" << QString::number(us, 16).leftJustified(2, ‘0‘);
    }
    qDebug() << "";

    //QChar *pcs = locallyEncoded.data();
    pcs = (QChar*)locallyEncoded.unicode();
    for (int i=0; i<locallyEncoded.length(); i++)
    {
        QChar c = pcs[i];
        ushort us = c.unicode();
        qDebug() << "\t" << QString::number(us, 16).leftJustified(2, ‘0‘);
    }

2、

时间: 2024-08-03 15:56:26

Qt532_字符编码转换的相关文章

Windows下字符编码转换

有时候经常使用别人用Tabhost+其它的实现demo.单纯利用Tabhost该如何使用呢? 下面看例子: public class MainActivity extends TabActivity { public TabHost tabHost; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 获取对象 tabHost = getTabH

erlang中字符编码转换(转)

转自:http://www.thinksaas.cn/group/topic/244329/ 功能说明: erlang中对各种语言的编码支持不足,此代码是使用erlang驱动了著名的iconv编码库来对字符进行编码转换处理. 文件说明: iconv_erl.c和iconv.h 是erlang字符编码模块的driver,作用是对iconv进行封装.编译后生成iconv_erl.dll,供iconv.erl使用. iconv_makefile.win32 windows上编译iconv_erl.dl

php 字符编码转换函数 iconv mb_convert_encoding比较

在使用PHP处理字符串时,我们经常会碰到字符编码转换的问题,你碰到过iconv转换失败吗? 发现问题时,网上搜了搜,才发现iconv原来有bug ,碰到一些生僻字就会无法转换,当然了配置第二个参数时,可以稍微弥补一下默认缺陷,不至于无法转换是截断,用法如下 iconv(“UTF-8″,”GB2312//IGNORE”,$data) ; 这样碰到生僻字转换失败时,它就会忽略失败,继续转换下面的内容,这算解决问题的一个办法,不过为了确保转换的成功率,我们可以用另一个转换函数(mb_convert_e

iconv字符编码转换

转自 http://blog.csdn.net/langresser_king/article/details/7459367 iconv(http://www.gnu.org/software/libiconv/)是一个开源的字符编码转换库,可以"方便"的完成几乎所有的编码转换工作.说简单是因为,它常用的接口就三个,iconv_open  iconv   iconv_close,但是即便是只有三个接口,要想使用正确也不容易.这里把一些基本概念和使用细节记录下来,希望能成为一篇最实用的

ASP中有关字符编码转换的几个有用函数

ASP中有关字符编码转换的几个有用函数 <%1.'UTF转GB---将UTF8编码文字转换为GB编码文字function UTF2GB(UTFStr) for Dig=1 to len(UTFStr)   '如果UTF8编码文字以%开头则进行转换  if mid(UTFStr,Dig,1)="%" then      'UTF8编码文字大于8则转换为汉字    if len(UTFStr) >= Dig+8 then        GBStr=GBStr & Con

php字符编码转换之gb2312转为utf8(转)

在php中字符编码转换我们一般会用到iconv与mb_convert_encoding进行操作,但是mb_convert_encoding在转换性能上比iconv要差很多哦.string iconv ( string in_charset, string out_charset, string str ) 注意:第二个参数,除了可以指定要转化到的编码以外,还可以增加两个后缀://TRANSLIT 和 //IGNORE,其中 //TRANSLIT 会自动将不能直接转化的字符变成一个或多个近似的字符

python基础 字符编码转换

python2 1 #python2上所有的字符编码都需要先decode到unicode,再从unicode encode到目标编码 2 str_utf8 = "我就是我" 3 print("str_utf-8:我就是我:",str_utf8) 4 #将utf-8转换为unicode 5 str_utf8_to_unicode = str_utf8.decode("utf-8") 6 print(str_utf8_to_unicode) 7 #将

C++ 字符编码转换类

记录一下C++ 编码转换的函数: 1 #pragma once 2 #include "afx.h" 3 4 5 #define DEFAULT_CODE 0 6 #define CHINESE_SIMPLIFIED 1 7 #define CHINESE_TRADITIONAL 2 8 9 class CChineseConvertor: 10 //public CObject 11 { 12 public: 13 CChineseConvertor(void); 14 ~CChin

字符编码转换笔记

何为字符编码? 字符编码为计算机文字的存储格式, 例如 英文 字母 以ASCII编码存储, 即单字节存储,  其他字符编码有 UTF-8(通用字符编码格式), 其他区域性编码格式, 例如 ISO-8859(西欧), windows-1251俄文,中文GB编码. 为什么需要转换? 正因各个地区有不同的编码格式, 为了交换信息的目的, 就需要将相同字符的 从一种编码格式 转换为 另外一种编码格式. 通用的编码格式为 UTF-8, 其囊括了 世界上所有字符, 所以一般为了通用性, 文件都以UTF-8编