Results 1 to 6 of 6

Thread: How to do QTextCodec conversion?

  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 do QTextCodec conversion?

    I have been struggling with character conversion, please help.

    I have a file in Chinese (GB2312), I need to read it and then print it on screen (in linux), I got garbage output on screen such as ��?��. I have tried for hours with different conversion. Nothing works!

    Here is the code:

    Qt Code:
    1. static QString toUnicode( const QString& str )
    2. {
    3. //return QTextCodec::codecForName( "GB2312" )->toUnicode( str.toLatin1() ).toLocal8Bit(); // <- not working
    4. //return QTextCodec::codecForName( "GB2312" )->toUnicode( str.toLatin1() ).toUtf8(); // <- not working
    5. return QTextCodec::codecForName( "GB2312" )->toUnicode( str.toLatin1() ); // <- not working
    6. }
    7.  
    8. void my_read_func()
    9. {
    10. QSettings settings( filename, QSettings::IniFormat );
    11. settings.setIniCodec( QTextCodec::codecForName( "GB2312" ) );
    12. settings.beginGroup( "xxxx" );
    13. {
    14. foreach( const QString& key, settings.allKeys() ) {
    15. QStringList strList = settings.value( key ).toStringList();
    16.  
    17. qDebug() << key << "=" << strList; // <- garbage out
    18.  
    19. foreach( const QString& str, strList ) {
    20. qDebug() << toUnicode( key ) << "=" << toUnicode( str ); // <- garbage out
    21. }
    22.  
    23. }
    24. }
    25. settings.endGroup();
    26. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to do QTextCodec conversion?

    Quote Originally Posted by lni View Post
    Qt Code:
    1. static QString toUnicode( const QString& str )
    2. {
    3. //return QTextCodec::codecForName( "GB2312" )->toUnicode( str.toLatin1() ).toLocal8Bit(); // <- not working
    4. //return QTextCodec::codecForName( "GB2312" )->toUnicode( str.toLatin1() ).toUtf8(); // <- not working
    5. return QTextCodec::codecForName( "GB2312" )->toUnicode( str.toLatin1() ); // <- not working
    6. }
    To copy to clipboard, switch view to plain text mode 
    That doesn't make any sense. Your str contains 16-bit Unicode already, what you want is some 8-bit encoding.
    If your shell/erminal is also using "GB2312" then you need to call the fromUnicode() function to generate the 8-bit version of that codec.

    Cheers,
    _

  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 do QTextCodec conversion?

    Quote Originally Posted by anda_skoa View Post
    That doesn't make any sense. Your str contains 16-bit Unicode already, what you want is some 8-bit encoding.
    If your shell/erminal is also using "GB2312" then you need to call the fromUnicode() function to generate the 8-bit version of that codec.

    Cheers,
    _
    How do I do this? My terminal is English locale with utf8. I try the following and still doesn't work

    qDebug() << QTextCodec::codecForName( "utf8" )->toUnicode( str.toAscii() ).toLocal8Bit();
    * qDebug() << QTextCodec::codecForName( "utf8" )->fromUnicode( filename.toLocal8Bit() );
    Thanks.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to do QTextCodec conversion?

    Quote Originally Posted by lni View Post
    qDebug() << QTextCodec::codecForName( "utf8" )->toUnicode( str.toAscii() ).toLocal8Bit();
    str.toAscii() will obviously do something very wrong if str does not contain ASCII.

    Quote Originally Posted by lni View Post
    * qDebug() << QTextCodec::codecForName( "utf8" )->fromUnicode( filename.toLocal8Bit() );
    So you convert the filename to local 8 bit encoding, which, according to you is UTF-8, then you transfer it back into 16bit Unicode and have qDebug() again encode it into 8 bit?

    Have you had a look at QString::toUtf8()?
    That is a short form of using th UTF-8 text codec and calling its fromUnicode() function.

    Cheers,
    _

  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 do QTextCodec conversion?

    Quote Originally Posted by anda_skoa View Post
    str.toAscii() will obviously do something very wrong if str does not contain ASCII.


    So you convert the filename to local 8 bit encoding, which, according to you is UTF-8, then you transfer it back into 16bit Unicode and have qDebug() again encode it into 8 bit?

    Have you had a look at QString::toUtf8()?
    That is a short form of using th UTF-8 text codec and calling its fromUnicode() function.

    Cheers,
    _
    Sorry, I probably should state more clear.

    I try to read an ini file using QSettings, the ini file contains GB2312 text.

    I want to read this file to work in all locale settings. The application may have done QTextCodec::setCodecForLocale( QTextCodec::codecForName( "GB2312" ) ), or may not have done that, in which case it is Linux English locale by default.

    The ini is like

    xxx=yyy

    which xxx is in GB2312, and yyy is also in GB2312. yyy is pointing to a file path, which I need to open the file to read.

    My approach is to decode both in UTF8 because the actual file name is in UTF8.

    Here is what I have done. I don't like temporarily set and restore the locale. It looks ugly.

    Qt Code:
    1. struct MyInfo {
    2. QString key;
    3. QString filename;
    4. };
    5.  
    6. static QString toUnicode( const QString& str ) {
    7. return QTextCodec::codecForName( "GB2312" )->toUnicode( str.toLatin1() );
    8. }
    9.  
    10.  
    11. * static MyInfo parseValue( const QString& key, const QStringList& strList ) {
    12. MyInfo info;
    13. info.key = key;
    14. info.filename = strList.first():
    15.  
    16. return info;
    17. }
    18.  
    19. static QMap<QString, MyInfo> info;
    20.  
    21. static void readIni( const QString& iniFileName ) {
    22. if( QFile::exists( iniFileName ) ) {
    23.  
    24. // save codec
    25. QTextCodec* codec = QTextCodec::codecForLocale();
    26.  
    27. // save codec utf-8 for this file
    28. QTextCodec::setCodecForLocale( QTextCodec::codecForName( "utf8" ) );
    29.  
    30. QSettings settings( iniFileName, QSettings::IniFormat );
    31. settings.setIniCodec( QTextCodec::codecForName( "GB2312" ) );
    32. settings.beginGroup( tag );
    33. {
    34. foreach( const QString& key, settings.allKeys() ) {
    35. QStringList strList = settings.value( key ).toStringList();
    36. QString localKey = toUnicode( key );
    37. MyInfo info = parseValue( localKey, strList );
    38. info[ info.name ] = info;
    39. }
    40. }
    41. settings.endGroup();
    42.  
    43. // restore codec (this looks ugly)
    44. QTextCodec::setCodecForLocale( codec );
    45. }
    46.  
    47. ...
    48.  
    49. }
    50.  
    51. // after reading the ini, I have to do like this:
    52.  
    53. static QImage createImage( const QString& key ) {
    54.  
    55. // save codec
    56. QTextCodec* codec = QTextCodec::codecForLocale();
    57. QTextCodec::setCodecForLocale( QTextCodec::codecForName( "utf8" ) );
    58.  
    59. QImage image( info[ key ].filename );
    60.  
    61. // restore codec (this looks ugly)
    62. QTextCodec::setCodecForLocale( codec );
    63.  
    64. return image;
    65. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to do QTextCodec conversion?

    Quote Originally Posted by lni View Post
    I try to read an ini file using QSettings, the ini file contains GB2312 text.

    The ini is like

    xxx=yyy

    which xxx is in GB2312, and yyy is also in GB2312. yyy is pointing to a file path, which I need to open the file to read.

    My approach is to decode both in UTF8 because the actual file name is in UTF8.
    How can yyy be in GB2312 and contain a UTF8 filename?
    Is yyy a character sequence with two different encodings?

    Qt Code:
    1. static QString toUnicode( const QString& str ) {
    2. return QTextCodec::codecForName( "GB2312" )->toUnicode( str.toLatin1() );
    3. }
    To copy to clipboard, switch view to plain text mode 
    That doesn't make any sense. str is already in unicode, that's how QStrings work internally.

    Cheers,
    _

Similar Threads

  1. Not existing Shift-JIS codec in QTextCodec
    By Sölve in forum Newbie
    Replies: 2
    Last Post: 8th June 2011, 13:48
  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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.