Results 1 to 4 of 4

Thread: Unicode/ASCII characters in QTextStream

  1. #1
    Join Date
    Aug 2008
    Posts
    18
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Unicode/ASCII characters in QTextStream

    When I read file contents into a QString like following:

    QFile myFile(test.u);
    myFile.open(QIODevice::ReadOnly);

    QTextStream ts(&myFile);
    QString strContent = ts.ReadAll();

    Is QTextStream smart enough to determine if the file content is Unicode or ASCII? Since QString holds data internally in Unicode format, I would get a totally different string if it cannot distinguish.

    Thanks!

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Unicode/ASCII characters in QTextStream

    From the docs:

    void QTextStream::setAutoDetectUnicode ( bool enabled )

    If enabled is true, QTextStream will attempt to detect Unicode encoding by peeking into the stream data to see if it can find the UTF-16 or UTF-32 BOM (Byte Order Mark). If this mark is found, QTextStream will replace the current codec with the UTF codec.

  3. The following user says thank you to squidge for this useful post:

    yren (23rd November 2009)

  4. #3
    Join Date
    Aug 2008
    Posts
    18
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Unicode/ASCII characters in QTextStream

    Quote Originally Posted by fatjuicymole View Post
    From the docs:

    void QTextStream::setAutoDetectUnicode ( bool enabled )

    If enabled is true, QTextStream will attempt to detect Unicode encoding by peeking into the stream data to see if it can find the UTF-16 or UTF-32 BOM (Byte Order Mark). If this mark is found, QTextStream will replace the current codec with the UTF codec.
    Thanks! How does the auto-detect know if it is Unicode file? Does Unicode file have some kind of header?

  5. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Unicode/ASCII characters in QTextStream

    It has an optional header. If the header is present and encoded with UTF-16 for example, the file will start with the bytes 0xFF,0xFE or 0xFE,0FF depending on byte order. The same happens for UTF-32, although there are 4 bytes instead of 2.

    If the header is missing, I don't see how it could be recognised easily. If you know its not a binary file, then any character >127 or certain characters below 32 could assume unicode.

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

    yren (23rd November 2009)

Similar Threads

  1. QTableView + sorting + numbers & special characters
    By foggy-mind in forum Qt Programming
    Replies: 5
    Last Post: 4th June 2009, 11:11
  2. when close QTextStream
    By mattia in forum Newbie
    Replies: 1
    Last Post: 24th November 2007, 13:17
  3. Replies: 13
    Last Post: 10th October 2007, 15:38
  4. Create QTextStream
    By Morea in forum Qt Programming
    Replies: 1
    Last Post: 17th June 2007, 20:25
  5. Problem at time compilation in traslation of language
    By thomasjoy in forum Qt Programming
    Replies: 3
    Last Post: 22nd May 2007, 14:18

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.