首页 > 其他 > 详细

Qt532_字符编码转换

时间:2016-05-26 17:13:14      阅读:180      评论:0      收藏:0      [点我收藏+]

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、

 

Qt532_字符编码转换

原文:http://www.cnblogs.com/cppskill/p/5531838.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!