精华内容
下载资源
问答
  • This may sounds like a obvious question, but I'm missing something about either how UTF-8 is encoded or how the toUtf8 function works.Let's look at a very simple programQString str("Müller");...

    This may sounds like a obvious question, but I'm missing something about either how UTF-8 is encoded or how the toUtf8 function works.

    Let's look at a very simple program

    QString str("Müller");

    qDebug() << str << str.toUtf8().toHex();

    Then I get the output

    "Müller" "4dc383c2bc6c6c6572"

    But I got the idea the the letter ü should have been encoded as c3bc and not c383c2bc.

    Thanks

    Johan

    解决方案

    It depends on the encoding of your source code.

    I tend to think that your file is already encoded in UTF-8, the character ü being encoded as C3 BC.

    You're calling the QString::QString ( const char * str ) constructor which, according to http://doc.qt.io/qt-4.8/qstring.html#QString-8, converts your string to unicode using the QString::fromAscii() method which by default considers the input as Latin1 contents.

    As C3 and BC are both valid in Latin 1, representing respectively à and ¼, converting them to UTF-8 will lead to the following characters:

    Ã (C3) -> C3 83

    ¼ (BC) -> C2 BC

    which leads to the string you get: "4d c3 83 c2 bc 6c 6c 65 72"

    To sum things up, it's double UTF-8 encoding.

    There are several options to solve this issue:

    1) You can convert your source file to Latin-1 using your favorite text editor.

    2) You can properly escape the ü character into \xFC in the litteral string, so the string won't depend on the file's encoding.

    3) you can keep the file and string as UTF-8 data and use QString str = QString::fromUtf8 ("Müller");

    Update: This issue is no longer relevant in QT5. http://doc.qt.io/qt-5/qstring.html#QString-8 states that the constructor now uses QString::fromUtf8() internally instead of QString::fromAscii(). So, as long as UTF-8 encoding is used consistently, it will be used by default.

    展开全文
  • QString 转toLatin1 toUtf8 toLocal8Bit toUtf8:utf8字符集以2个或以上的字节表示一个汉字。实际上具体的数值和unicode有很大的相关 toLatin1:ISO-8859-1编码是单字节编码,向下兼容ASCII,其编码范围是0x00-0xFF ...

    QString 转toLatin1 toUtf8 toLocal8Bit
    toUtf8:utf8字符集以2个或以上的字节表示一个汉字。实际上具体的数值和unicode有很大的相关
    toLatin1:ISO-8859-1编码是单字节编码,向下兼容ASCII,其编码范围是0x00-0xFF
    toLocal8Bit:gb18030字符集兼容了gbk字符集,以两个字节表示一个文字。windows系统可能使用的就是这两种的一种

    展开全文
  • const QByteArray& filebyte=file.toUtf8(); media=0; media = libvlc_media_new_path(instance,filebyte.data()); 但是同样的代码,qt4可以播放中文路径下的电影,这是什么原因呢? 求大神帮忙,谢谢你们 了!!...
  • QByteArray QString Base64 UTF8

    千次阅读 2016-05-12 09:53:11
    QByteArray bytearray64 = jsonValue.toString().toUtf8(); QByteArray bytearraytheme = QByteArray::fromBase64(bytearray64);  QString temp = QString::fromUtf8(bytearraytheme);
    QByteArray bytearray64 = jsonValue.toString().toUtf8();
    QByteArray bytearraytheme = QByteArray::fromBase64(bytearray64);

                            QString temp = QString::fromUtf8(bytearraytheme);

    1. QByteArray cstr("abcd");  
    2. QString s = cstr;   
    3.   
    4. QByteArray cstr("abcd");  
    5. QString s;  
    6. s.prepend(cstr);  
    7.   
    8. QString s("hello");  
    9. QByteArray cstr = s.toAscii();  
    10.   
    11. QString s("hello");  
    12. QByteArray cstr = s.toLatin1();  

    QByteArray类同样不以'\0'为结尾:如

    [cpp]  view plain  copy
      在CODE上查看代码片 派生到我的代码片
    1. QByteArray s;  
    2. s.resize(5);  
    3. s[0] = '1';  
    4. s[1] = '2';  
    5. s[2] = '3';  
    6. s[3] = '\0';  
    7. s[4] = 'a';  
    8.   
    9. cout << s << endl;  

    第一种,数据流的方式,这里只说从QByteArray转向QString。
    QByteArray encodedString = "xxx";
    QTextCodec *codec = QTextCodec::codecForName("KOI8-R");
    QString string = codec->toUnicode(encodedString);
    第二种,常用的,网上看到的。
    //常用参数类型:char *字符串, QByteArray字符数组, QString字符串
    //需要转换:char * ---转--- QByteArray ---需要调用QByteArray类的构造函数
    char*     --------------QByteArray(const char*)-----------> QByteArray
    char* str;
    QByteArray byte(str);
    char*     --------------QString(const char*)-------------->QString
    char* str;
    QString string(str);
    QByteArray   -------------data()-------------------->char*
    QByteArray byte;
    char* str = byte.data();
    QByteArray ------------QString()------------------->QString
    QByteArray byte;
    QString string(byte);
    QString --------------toAscii()--------------------> QByteArray
    QString string;
    QByteArray byte = string.toAscii();
    QString -------------qPrintable()------------------>char*
    QString string;

    char* str = qPrintable(string);


    展开全文
  • QString to base64QString , to unBase64QString

    千次阅读 2018-12-08 18:38:37
    QString qs2B64(QString qs1){//ok ////////////////////////////// QByteArray ba; ba=qs1.toUtf8(); //QByteArray ba=ba.toBase64(); //Base64 char * cx=ba.d...
    QString qs2B64(QString qs1){//ok
        //
        QByteArray ba;
        ba=qs1.toUtf8();           //QByteArray
        ba=ba.toBase64();          //Base64
        char * cx=ba.data();       //char *
        QString b64qs1=QString(cx);//QString
        //
        return b64qs1;
    }
    QString b64ToQs(QString b64qs1){//ok
        QByteArray ba;
        /
        std::string stdStr = b64qs1.toStdString();//std::string
        ba=QByteArray(stdStr.c_str() );           //QByteArray
        ba=ba.fromBase64(ba);                     //unBase64
        /
        QString qs1=QString::fromUtf8(ba);        //QString
        return  qs1;
    }
    展开全文
  • 支持中文收发,默认GB2312编码,如需Unicode/UTF8/UTF16需要更改代码。(巨重要) https://bbs.csdn.net/topics/350007957
  • To convert QString to LPTSTR or LPCTSTR:

    千次阅读 2012-06-14 13:03:16
    To convert QString to LPTSTR or LPCTSTR: QString src; LPTSTR dest=(LPTSTR)src.utf16; to convert from LPTSTR or LPCTSTR to QString : src=QString::fromUtf16(dest);
  • 公司代码有多国语言支持,遇到需要...从Qt5开始只剩下setCodecForLocale这一个了,只是影响Qt对toLocal8Bit相关函数的编码方式 QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8")); QTextCodec...
  • QStringtoLocal8bit与toLatin1的区别

    千次阅读 2020-12-09 15:09:37
    问题缘由:将QString赋值给char数组。 方法:QString→QByteArray→char数组 QString str("123456"); QByteArray arr = str.toLocal8Bit();//Unicode编码 //QByteArray arr = str....QStringtoLocal8bit和toLat
  • #ifndef _QT_UTF8_H #define _QT_UTF8_H #include &lt;QString&gt; #include &lt;string&gt; using std::string; class AfUtf8 { public: // QString(Unicode) -&gt; std::str...
  • QString里面的汉字是UTF-8编码的字符集,QString::toUtf8是输出UTF-8编码的字符集 QString::toLatin1是相当与ASCii码不包含中文的遇到中文默认转换为ascii码0x3f也就’?‘ QString::Local8bit是本地操作系统设置...
  • How to convert a QString to unicode object in python 2? I had this problem to solve, and I tried to find the safest way. This program illustrates the solution from PyQt4 import QtCore, QtGui ...
  • Utf16ToUtf8

    千次阅读 2010-07-07 16:21:00
    摘自Qt源码: typedef unsigned char Uint8; typedef signed char Int8; typedef unsigned short Uint16; typedef signed short Int16; typedef signed int Int;...size_t utf16ToUtf8( char* dest, con
  • 1.std::string 转 QString QString StrToQStr(const char* s) { if ( NULL == code ) { code = QTextCodec::codecForName("GBK"); if ( NULL == code ) { code = QTextCodec::codecForName("GB18030");...
  • QString

    千次阅读 2019-08-16 13:53:19
    这实际上是使用QString::fromUtf8()将一个C语言风格的字符串: const char * p = "hello"; 转成QString。 p可以为空,以下写法是合法的: #define debug qDebug()<< int main(int argc, char *argv[]) {...
  • QByteArray(UTF8编码串)转QString

    千次阅读 2013-04-04 21:39:56
    QTextCodec *tc = QTextCodec::codecForName("UTF8"); QByteArray ba = 读到的QByteArray串; QString str = tc->toUnicode(ba); cout 这个时候,就能够正常显示中文了。
  • QString.toUtf8() -> QByteArray trimmed(...) QString.trimmed() -> QString truncate(...) QString.truncate(int) ``` ``` 使用的环境是PyQt4-4.11.4-gpl-Py2.7-Qt4.8.7 和 Python2.7 这种问题该如何...
  • toLocal8Bit() 是将QString以自己正确的编码方式读取并转换成编译器编码方式的字节流,   而string本身就是字节流没有编码方式,等需要他时编译器会以编译器的编码读取方式读取并且没什么问题,至于显示是否是...
  • Qstring

    千次阅读 2018-07-12 10:57:35
    原地址:https://blog.csdn.net/qq_40194498/article/details/79629632QString在Qt的各种数据转换中可谓举足轻重,熟悉QString的用法对于Qt编程真的是如虎添翼:QString 字符串有相当多的操作符和函数,此处只介绍...
  • qt中的toUtf8, toLatin1, Local8bit, toUcs4

    万次阅读 多人点赞 2015-12-15 22:58:59
    1 首先说下字符集。 gb18030字符集兼容了gbk字符集,以两个字节表示一个文字。windows系统可能使用的就是这两种的一种。 unicode字符集以2个或以上的字节表示一个汉字。 通用字符集(Universal Character Set, UCS)...
  • 源码必须是UTF-8QString需要它

    千次阅读 2013-09-09 14:34:54
    来源: http://blog.qt.digia.com/cn/2012/05/16/source-code-must-be-utf-8-and-qstring-wants-it/ ...Source code must be UTF-8 and QString wants it 先前我讨论过源代码的编码问题,认为C++语言缺少一个必要的
  • QString 中文编码转换

    万次阅读 2017-07-10 22:17:26
    QString GBK2UTF8(const QString &str) { QTextCodec *utf8 = QTextCodec::codecForName("UTF-8");... return utf8->toUnicode(str.toUtf8()); } QString UTF82GBK(const QString &str) { QTextCodec *gbk
  • #include #include #include #include void MainWindow::on_pushButton_clicked() {   /*  只有8位编码的才需要tr, unicode不要tr ... 所谓的QString转char*,结果并不相同,根据编码
  • qstring

    千次阅读 2010-10-30 21:17:00
    所有的QString的方法都使用const char *参数,const char *被解释为经典的C风格的以零结尾的ASCII字符串。所以const char *参数为0是合法的。如果const char *不是以零结尾的,结果是不确定的。把经典的C字符...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 7,021
精华内容 2,808
关键字:

qstringtoutf8