Results 1 to 9 of 9

Thread: reading 4-bytes integer from binary file

  1. #1
    Join Date
    May 2009
    Posts
    7
    Thanks
    3

    Default reading 4-bytes integer from binary file

    Hello, people.

    Нelp me please with subject.
    Im a beginner in QT. It looks like QT has only 8/16/32/64-bytes integer. Is there is a way to read 4-bytes integer from binary file in QT?

    Thank you!

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    507
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: reading 4-bytes integer from binary file

    Those numbers are bits, not bytes.

    Ginsengelf

  3. #3
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: reading 4-bytes integer from binary file

    And 8 bits is 1 byte so you are looking for 32-bit integer which is exactly 4-byte integer. In Qt it's qint32.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  4. #4
    Join Date
    May 2009
    Posts
    7
    Thanks
    3

    Default Re: reading 4-bytes integer from binary file

    Oh! Im just an idiot!
    Thank you for illumination.

  5. #5
    Join Date
    May 2009
    Posts
    7
    Thanks
    3

    Default Re: reading 4-bytes integer from binary file

    Ok. Illumination is good but the code still doesnt work.

    Qt Code:
    1. QFile Source_file;
    2. Source_file.setFileName("D:/Dela/QTtest/Begin.rg2");
    3. if (Source_file.open(QIODevice::ReadOnly)) {
    4. QDataStream data (&Source_file);
    5. // data.setByteOrder(data.LittleEndian);
    6. qint32 intb;
    7. data>>intb;
    8. qDebug()<<intb;
    9. }
    10. Source_file.close();
    To copy to clipboard, switch view to plain text mode 

    I have a value of intb different from that was saved in file. I tried both LittleEndian and BigEndian.
    The file was made in Delphi. It reads well in Delphi and in Java.

    What the problem may be in?

    Thank you.
    Last edited by wysota; 8th May 2009 at 07:19. Reason: missing [code] tags

  6. #6
    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: reading 4-bytes integer from binary file

    Quote Originally Posted by maka View Post
    What the problem may be in?
    The problem is in using QDataStream. It's a serialization mechanism that stores (and expects) some additional data in the stream. If you just want to read 4 bytes from the file, use QIODevice::read on the QFile object and cast the data to a type of your choice.
    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.


  7. #7
    Join Date
    May 2009
    Posts
    7
    Thanks
    3

    Default Re: reading 4-bytes integer from binary file

    Quote Originally Posted by wysota View Post
    The problem is in using QDataStream. It's a serialization mechanism that stores (and expects) some additional data in the stream. If you just want to read 4 bytes from the file, use QIODevice::read on the QFile object and cast the data to a type of your choice.
    Thanks for reply!

    Now Im trying to do this:

    Qt Code:
    1. QFile Source_file;
    2. Source_file.setFileName("D:/Dela/QTtest/Begin.rg2");
    3.  
    4. if (Source_file.open(QIODevice::ReadOnly)) {
    5. qint32 intb;
    6. bar = Source_file.read(4);
    7. bool ok;
    8. intb = bar.toInt(&ok,0);
    9. qDebug()<<intb;
    10.  
    11. }
    12. Source_file.close();
    To copy to clipboard, switch view to plain text mode 

    Still have wrong result. I suspect becouse of endianity. Is there is a way to fix it avoiding manual changing of bytes order?
    Last edited by maka; 8th May 2009 at 09:37.

  8. #8
    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: reading 4-bytes integer from binary file

    toInt() does something completely different. What you need to do is something like this:
    Qt Code:
    1. QByteArray ba = file.read(4);
    2. qint32 x;
    3. memcpy(&x, ba.constData(), 4);
    To copy to clipboard, switch view to plain text mode 
    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.


  9. The following user says thank you to wysota for this useful post:

    maka (12th May 2009)

  10. #9
    Join Date
    May 2009
    Posts
    7
    Thanks
    3

    Default Re: reading 4-bytes integer from binary file

    It works.

    Thank you very much!

Similar Threads

  1. Read binary from file
    By weldpua2008 in forum Newbie
    Replies: 2
    Last Post: 3rd April 2009, 23:50
  2. cannot execute binary file
    By mgturner in forum Installation and Deployment
    Replies: 1
    Last Post: 16th March 2009, 17:04
  3. File Reading Advice
    By tntcoda in forum Qt Programming
    Replies: 1
    Last Post: 11th November 2008, 19:44
  4. Reading binary data
    By vermarajeev in forum Qt Programming
    Replies: 1
    Last Post: 13th August 2007, 09:14
  5. Sending Binary File with QFTP
    By nbkhwjm in forum Newbie
    Replies: 2
    Last Post: 7th March 2007, 18:10

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.