Results 1 to 5 of 5

Thread: Help needed to convert unicode characters

  1. #1
    Join Date
    Oct 2009
    Posts
    22
    Thanks
    2
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Question Help needed to convert unicode characters

    Hi it's been a while, new job new challenge and getting stuck into Qt goodness for a living.

    I am having some trouble with a QString that is filled with a JSON structure, in amongst that there are some french words and the accented characters in those words are being replaced with encoding looking like /00e8. I have tried the obvious using toUtf8 even converting the QString into a const char* and trying to use QString::fromUtf8, but still qDebug prints the encoded character in unicode form.

    Am I doing doing something fundamentally wrong here?

    Thanks

    H

  2. #2
    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: Help needed to convert unicode characters

    This works fine on my Linux box.
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QDebug>
    3.  
    4. int main(int argc, char **argv)
    5. {
    6. QCoreApplication a(argc, argv);
    7. QString test1 = QString::fromUtf8("\xc3\xa8"); // è encoded in UTF8
    8. QString test2(QChar(0x00e8)); // or directly as Unicode code point number
    9. qDebug() << test1 << test2;
    10. // outputs "è" "è"
    11. return 0;
    12. }
    To copy to clipboard, switch view to plain text mode 

    You need to be very clear exactly what is in the source data, and what encoding it is in etc., and what only appears in the output because of something you have done or limitations of the output device.

    If the JSON string actually contains the six characters '\', 'u', '0', '0', 'e', '8' then that is a very different thing again.

  3. #3
    Join Date
    Oct 2009
    Posts
    22
    Thanks
    2
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Help needed to convert unicode characters

    Thanks, I thought I'd better give some context in the way the JSON is used. I have tried converting raw back to a const char* and using QString::fromUtf8 but that doesn't work either.

    As you can see the QString does contain \u00e9, I guess what is going on here would be like if "\n" was treat as two separate bytes and printed as '\' 'n'.

    Qt Code:
    1. struct JSONProcessor : public RPCRequest::Processor
    2. {
    3. virtual QVariant process( const QString& raw )
    4. {
    5. // This is what raw looks like
    6. // "{\"data\":[{\"id\":\"535666\",\"readable\":true,\"title\":\"B\u00e9linda\"}]}";
    7.  
    8. //this doesn't work
    9. //qDebug() << QString::fromUtf8(raw);
    10.  
    11. return someProcessing( raw );
    12. }
    13. };
    To copy to clipboard, switch view to plain text mode 

  4. #4
    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: Help needed to convert unicode characters

    OK. Nothing in the standard Qt bag-o-tricks will do this for you. You need to disassemble and un-escape the escaped elements, e.g. \" or \u00e9, yourself using QRegExp, QString etc. (see http://www.ietf.org/rfc/rfc4627.txt) or use something like QJson (LGPL) to do it for you. I would go the second route unless licences prohibit you.
    Last edited by ChrisW67; 5th July 2012 at 07:18.

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

    hybrid_snyper (24th August 2012)

  6. #5
    Join Date
    Oct 2009
    Posts
    22
    Thanks
    2
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Help needed to convert unicode characters

    Just thought I would close this thread off and point to the solution that helped me. Turned out the API I was talking to was returning a 'unicode' string representation of special characters and not unicode.

    Anyways this helped http://www.qtcentre.org/threads/3731...icode-Problems

Similar Threads

  1. Replies: 0
    Last Post: 7th June 2012, 22:58
  2. Replies: 6
    Last Post: 30th March 2012, 09:01
  3. QString to unicode characters
    By jsmax in forum Newbie
    Replies: 1
    Last Post: 19th September 2011, 11:41
  4. Unicode/ASCII characters in QTextStream
    By yren in forum Qt Programming
    Replies: 3
    Last Post: 23rd November 2009, 19:25
  5. Insertion of unicode characters into database oracle through pro c
    By hemananda choudhuri in forum Qt Programming
    Replies: 1
    Last Post: 8th January 2007, 11:42

Tags for this Thread

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.