Results 1 to 6 of 6

Thread: How to use QTextCodec

  1. #1
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to use QTextCodec

    Hi,

    I have a following string

    "²¹Ðĺ£?#xce;"

    What does this mean? How can I use QTextCodec to convert it to readable characters?

    Thanks!

  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: How to use QTextCodec

    Exactly how many "characters" are in that string? I think the forum is playing with your input. I see literally this:
    "²¹Ðĺ£?#xce;"
    But is suspect you pasted some smaller number of "gibberish" characters.
    What does this mean?
    It is impossible to say out of context. This is part of the reason that Unicode is so useful.
    How can I use QTextCodec to convert it to readable characters?
    Impossible to know without knowing where the source material originated and what encoding was used.

  3. #3
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to use QTextCodec

    Sorry, don't know what more information I should give.

    It is supposed to be some Chinese characters. I think each block of ";" is a character. Normally they are readable Chinese characters that I can display in QTextEdit, but then I am hit by this unreadable string, and can't display it properly in QTextEdit.

    The encoding is supposed to be GB2312...

  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: How to use QTextCodec

    Do you literally have "²¹Ðĺ£?#xce;" in your string? Am I correct in assuming the "?" is a typo and should be "&"?

    Edit: The "&#xhh;" is an HTML/XML escaped byte with hexadecimal ("x") value of hh.

    GB2312 is a character set that can be encoded several ways. The "GB18030" QTextCodec is what you probably want to use:
    Qt Code:
    1. #include <QtCore>
    2.  
    3. int main(int argc, char **argv)
    4. {
    5. QCoreApplication app(argc, argv);
    6. QByteArray encodedString = QByteArray::fromHex("b2b9d0c4baa3ce");
    7. QTextCodec *codec = QTextCodec::codecForName("GB18030");
    8. QString string = codec->toUnicode(encodedString);
    9. qDebug() << string;
    10. // Outputs three Chinese characters:
    11. // "补心海"
    12. return 0;
    13. }
    To copy to clipboard, switch view to plain text mode 
    Note that the last byte in the input string is an incomplete character. Does the result look correct?
    Last edited by ChrisW67; 20th December 2013 at 00:19. Reason: Expanded

  5. #5
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to use QTextCodec

    Wow! Thanks! It is "补心海拔".

    But now in some place I am getting normal string like "补心海拔", in other place I get that unreadable string. How can I tell in my program to handle both cases correctly?

    Edit 1: "?" is not a typo, I copy straight from what I get.

    Edit 2: It should be 4 Chinese characters, but I only get 3 with your code...

    Edit 3: even worse than this. I am getting mixed string like this "X?#xf8;æ ‡", which should be “Xåæ ‡â€. I suppose the user has broken database. But the user don't care, they blame me! So I have to find a workaround.

    Edit 4: further investigation, I found that the "?" represents missing string, in this example, "?" should be "&#xb0;&", so the complete string should be (I think) "&#xb2;&#xb9;&#xd0;&#xc4;&#xba;&#xa3;&#xb0;&#xce;" . The it gives "补心海拔".

    With all these mess, what should I do? Just tell user that they have bad database and ask then to fix their database? I think they would not be happy to hear that...


    Thank you!
    Last edited by lni; 20th December 2013 at 11:36.

  6. #6
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to use QTextCodec

    OK,

    The user agrees it is their database problem. Thank you ChrisW67!

Similar Threads

  1. How to do QTextCodec conversion?
    By lni in forum Qt Programming
    Replies: 5
    Last Post: 19th October 2013, 14:55
  2. QIODevice and QTextCodec?
    By whitefurrows in forum Qt Programming
    Replies: 5
    Last Post: 26th November 2010, 18:50
  3. QTextCodec bad encoding?
    By ComaWhite in forum Newbie
    Replies: 0
    Last Post: 18th September 2009, 06:15
  4. Question in QTextCodec.
    By morgana in forum Newbie
    Replies: 1
    Last Post: 21st September 2008, 16:29
  5. QTextCodec::setCodecForCStrings() in libraries
    By conexion2000 in forum Qt Programming
    Replies: 5
    Last Post: 10th August 2007, 21:21

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
  •  
Qt is a trademark of The Qt Company.