Results 1 to 4 of 4

Thread: Reading string in Utf8 from binary file

  1. #1
    Join Date
    Jan 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Reading string in Utf8 from binary file

    Hi,
    I'm trying to read a string in Utf8 encoding from binary file through using QDataStream.

    Binary file has some records like this:
    Qt Code:
    1. 06000000D098D0BCD18F
    To copy to clipboard, switch view to plain text mode 
    where
    06000000 - quint32, text data size in bytes,
    D098D0BCD18F - text in utf8, just a 3 chars in russian

    I'm trying to do like this:
    Qt Code:
    1. QDataStream in(&file);
    2. ...
    3. quint32 labelLen;
    4. in >> labelLen;
    5. qDebug() << "labelLen=" << labelLen;
    6.  
    7. char * label = new char [labelLen];
    8. in.readRawData(label, labelSize);
    9. qDebug() << "label=" << label;
    To copy to clipboard, switch view to plain text mode 

    But as a result in console I'm getting some unreadable data, not a text...

    How to read a data correctly and convert readed a text data to QString?
    Last edited by iddqd; 17th July 2010 at 13:31.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Reading string in Utf8 from binary file

    Using readRawData is wrong.

    Quoting the documentation:
    The data is not encoded.
    This means that the text is not read as UTF8, instead, the character "D0 98" is not read as one character but as two distinct characters. This is why you don't see Cyrillic text.

    To overcome this, I advise to use a QTextStream to read the UTF encoded data in the file.

  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: Reading string in Utf8 from binary file

    Check your byte order. QDataStream is designed to read streams written by QDataStream. This code:
    Qt Code:
    1. quint32 labelLen = 6;
    2.  
    3. QBuffer buf(&arr);
    4. buf.open(QIODevice::WriteOnly);
    5. QDataStream s(&buf);
    6. s << labelLen;
    7. buf.close();
    8.  
    9. qDebug() << arr.toHex();
    To copy to clipboard, switch view to plain text mode 
    outputs "00000006" not "06000000". Your labelLen figure is 100663296 rather than 6.

  4. #4
    Join Date
    Jan 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Reading string in Utf8 from binary file

    Thanks for answer. Byte order is ok, labelLen=6. I've already solved a problem by using
    Qt Code:
    1. qDebug() << "label=" << QString::fromUtf8(label, labelLen);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Binary file reading using Structure
    By umulingu in forum Qt Programming
    Replies: 6
    Last Post: 25th July 2009, 11:35
  2. Reading/writing data to binary file
    By DiamonDogX in forum Qt Programming
    Replies: 3
    Last Post: 23rd July 2009, 19:24
  3. Reading characters in Binary file.....
    By umulingu in forum Qt Programming
    Replies: 2
    Last Post: 23rd July 2009, 04:51
  4. Binary file Reading.........
    By umulingu in forum Qt Programming
    Replies: 11
    Last Post: 20th July 2009, 06:18
  5. reading 4-bytes integer from binary file
    By maka in forum Qt Programming
    Replies: 8
    Last Post: 12th May 2009, 05:57

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.