Results 1 to 5 of 5

Thread: QT4.6 Converting utf8 to '/uwxyz' and back.

  1. #1
    Join Date
    Nov 2016
    Posts
    2
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default QT4.6 Converting utf8 to '/uwxyz' and back.

    I need to convert ut8 (currently read in by a QTextStream readLine() ) to ascii text:
    1) if ascii (<=7F), then just use the ascii value so 'a' to 'a'
    2) if not ascii then convert utf8 to '/u' followed by the hex value wxyz.
    For example the Euro symbol would be the 6 ascii charaters '/u20AC'

    I also have to go the other way where I have the string '/u20AC' and want to output as utf8 for the Euro.

    I am having troubles determining whether QT string functions can help me with this or not.

    It looks like if I use 'toUtf8 I will get a byte array with the euro as bytes E2 82 AC and could parse manually, but that is a bunch of work.

    Is there a way I can get the unicode hex values from the utf8 QString?

  2. #2
    Join Date
    Oct 2016
    Posts
    61
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QT4.6 Converting utf8 to '/uwxyz' and back.

    i tried it but i couldn't too

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QT4.6 Converting utf8 to '/uwxyz' and back.

    There is no such beast as a "utf8 QString". QString is a collection of QChar, essentially 16-bit Unicode basic multilingual plane code points that are trivially accessible using QString::at() or other methods. The file or stream you are reading from may be UTF-8 encoded and decoded by QTextStream.

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QFile>
    3. #include <QTextStream>
    4. #include <QDebug>
    5.  
    6. int main(int argc, char **argv)
    7. {
    8. QCoreApplication app(argc, argv);
    9.  
    10. QFile file("test.txt");
    11. if (file.open(QIODevice::ReadOnly)) {
    12. QTextStream in(&file);
    13. in.setCodec("UTF-8");
    14. QString line = in.readLine();
    15.  
    16. qDebug() << line;
    17.  
    18. QString result;
    19. for(int i = 0; i < line.size(); ++i) {
    20. const ushort code = line.at(i).unicode();
    21. if (code < 0x0080)
    22. result += line.at(i);
    23. else
    24. result += QString("\\u%1").arg(code, 4, 16, QChar('0'));
    25. }
    26.  
    27. qDebug() << result;
    28. }
    29. return 0;
    30. }
    To copy to clipboard, switch view to plain text mode 
    Output:
    Qt Code:
    1. "test €1234"
    2. "test \u20ac1234"
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to ChrisW67 for this useful post:

    Greebley (11th November 2016)

  5. #4
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QT4.6 Converting utf8 to '/uwxyz' and back.

    ... as far as the line QString contains unicode characters below 0xFFFF (in fact, below 0xD800). The unicode in Qt is, in fact UTF16 and the characters above 0xFFFF are encoded as two ushorts. If the characters above 0xFFFF threaten, then use a small improvement:
    Qt Code:
    1. #include <QVector>
    2. ...
    3. QString line = in.readLine();
    4.  
    5. qDebug() << line;
    6.  
    7. QString result;
    8. QVector<uint> utf8 = line.toUcs4();
    9.  
    10. for( int i = 0; i < utf8.size(); ++i )
    11. {
    12. const uint code = utf8.at(i);
    13.  
    14. if( code < 0x0080 ) result += line.at(i);
    15. else result += QString("\\u%1").arg(code, 4, 16, QChar('0'));
    16. }
    17.  
    18. qDebug() << result;
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to Radek for this useful post:

    Greebley (11th November 2016)

  7. #5
    Join Date
    Nov 2016
    Posts
    2
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QT4.6 Converting utf8 to '/uwxyz' and back.

    I have gotten it to work using the ideas here:
    Setting the QTextStream to utf8 and then working character by character

Similar Threads

  1. Replies: 2
    Last Post: 7th September 2010, 21:19
  2. decoding from utf8
    By pcaeiro in forum Qt Programming
    Replies: 6
    Last Post: 25th August 2009, 12:51
  3. UTF8 and QStandardItem?
    By alexandernst in forum Newbie
    Replies: 17
    Last Post: 26th July 2009, 18:29
  4. UTF8 and ByteOrderMark (BOM)
    By SidGBF in forum Qt Programming
    Replies: 1
    Last Post: 18th August 2008, 22:25
  5. Utf8 problems
    By cristiano in forum Qt Programming
    Replies: 5
    Last Post: 11th November 2006, 00:14

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.