Results 1 to 7 of 7

Thread: Unicode, linux, bash shell, printf

  1. #1
    Join Date
    Apr 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Unicode, linux, bash shell, printf

    This must be really simple... I have a unicode QString and I would like to print the string to the shell (konsole, bash). The console has the appropriate characters so touch and mkdir can create unicode file names -> printing unicode characters must not be the problem. What I have:
    Qt Code:
    1. QString s = "áéőűÁÉŐŰ";
    To copy to clipboard, switch view to plain text mode 

    How do I print that string to the console that those characters appear?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Unicode, linux, bash shell, printf

    What encoding that your console use?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  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: Unicode, linux, bash shell, printf

    Your test string of eight "characters" is represented as the sixteen bytes (hex):
    Qt Code:
    1. á é ő ű Á É Ő Ű
    2. C3 A1 C3 A9 C5 91 C5 B1 C3 81 C3 89 C5 90 C5 B0
    To copy to clipboard, switch view to plain text mode 
    when encoded as UTF-8 (which is the most likely thing your editor has done). Constructing or assigning a QString from char* uses fromAscii() which will be misinterpreting the bytes when converting to QString's internal 16-bit character format. Try this:
    Qt Code:
    1. QString s = QString::fromUtf8("áéőűÁÉŐŰ");
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Apr 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Unicode, linux, bash shell, printf

    Well, hmm, it does not quite work. Actually the problem is not how to put into QString, it is the easy part. The problem is how to actually print.
    Technically qDebug() << str; works fine for unicode; however qDebug, qWarning, etc. are overwritten by installing a different message handler.

    The question is very simple:
    Qt Code:
    1. QString s = "őű";
    2. // ????: printf("%s\n", s);
    3. // ????: printf("%s\n", s.toAscii());
    4. // ????: etc.
    5. // What to put there instead of printf?
    To copy to clipboard, switch view to plain text mode 

    console used: konsole

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Unicode, linux, bash shell, printf

    Please answer my question --- what encoding does your console use. If it's utf-8 then qDebug() << str.toUtf8() should work. If it's something else then probably str.to8BitEncoding() will work.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Apr 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Unicode, linux, bash shell, printf

    I tried both of those down below. Any idea would help.

    Qt Code:
    1. static QTextCodec *pCodec = QTextCodec::codecForName("UTF-8");
    2.  
    3. // pCodec is available through the application; does not go out of scope, not that it would even matter
    4.  
    5. if (pCodec == 0) {
    6. qCritical("UTF-8 is not supported by the O/S or shell");
    7. return;
    8. }
    9.  
    10. QTextCodec::setCodecForCStrings(pCodec);
    11. QTextCodec::setCodecForLocale(pCodec);
    12. QTextCodec::setCodecForTr(pCodec);
    13.  
    14. // Print here with anything else but qDebug, qWarning, qCritical and qFatal
    To copy to clipboard, switch view to plain text mode 

    The default encoding of Konsole on CentOS is UTF-8; and that is used.


    Added after 28 minutes:


    Actually, solved. I think build was not cleaned properly.
    Last edited by sandor; 19th July 2012 at 23:11.

  7. #7
    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: Unicode, linux, bash shell, printf

    Quote Originally Posted by sandor View Post
    Well, hmm, it does not quite work. Actually the problem is not how to put into QString, it is the easy part.
    I don't think you understand the problem.

    The problem is how to actually print.
    The question is very simple:
    Qt Code:
    1. QString s = "őű";
    2. // ????: printf("%s\n", s);
    3. // ????: printf("%s\n", s.toAscii());
    4. // ????: etc.
    5. // What to put there instead of printf?
    To copy to clipboard, switch view to plain text mode 
    Well, printf() will work, and so will std::cout, QTextStream and qDebug but you'll will only see the characters you expect if they get into the QString correctly in the first place.

    Qt Code:
    1. #include <QtCore>
    2. #include <QDebug>
    3. #include <iostream>
    4.  
    5. void output(const QString &s) {
    6. printf("%s\n", s.toUtf8().data());
    7. std::cout << s.toUtf8().data() << std::endl;
    8. QTextStream out(stdout);
    9. out << s << endl;
    10. qDebug() << s;
    11. qDebug() << "-----";
    12. }
    13.  
    14.  
    15. int main(int argc, char **argv)
    16. {
    17. QCoreApplication app(argc, argv);
    18.  
    19. QString s("áéőűÁÉŐŰ"); // mangled by toAscii()
    20. output(s);
    21. s = "áéőűÁÉŐŰ"; // mangled by toAscii() too
    22. output(s);
    23. s = QString::fromUtf8("áéőűÁÉŐŰ"); // not mangled
    24. output(s);
    25.  
    26. return 0;
    27. }
    To copy to clipboard, switch view to plain text mode 

    Just for reference I am using Konsole with UTF8 encoding and suitable fonts. This is the output:
    Qt Code:
    1. áéÅűÃÃÅÅ°
    2. áéÅűÃÃÅÅ°
    3. áéÅűÃÃÅÅ°
    4. "áéÅűÃÃÅÅ°"
    5. -----
    6. áéÅűÃÃÅÅ°
    7. áéÅűÃÃÅÅ°
    8. áéÅűÃÃÅÅ°
    9. "áéÅűÃÃÅÅ°"
    10. -----
    11. áéőűÁÉŐŰ
    12. áéőűÁÉŐŰ
    13. áéőűÁÉŐŰ
    14. "áéőűÁÉŐŰ"
    15. -----
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 2
    Last Post: 14th March 2011, 20:55
  2. Replies: 11
    Last Post: 21st February 2011, 17:54
  3. Accesing linux shell script from Qt
    By Ricardo_arg in forum Qt Programming
    Replies: 2
    Last Post: 17th February 2011, 12:49
  4. QProcess & linux shell characters
    By Ti_Thom in forum Qt Programming
    Replies: 4
    Last Post: 21st December 2009, 10:01
  5. Linux/Bash: Run a command as another user
    By sunil.thaha in forum General Discussion
    Replies: 1
    Last Post: 5th December 2006, 09:49

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.