Results 1 to 8 of 8

Thread: Help with Binary reader in qt ???

  1. #1
    Join Date
    Aug 2010
    Posts
    36
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Help with Binary reader in qt ???

    Hi,

    I need help with reading binary files in Qt.
    Following is something in dotnet and I need to implement similar stuff using Qt
    ----------------------------------------------------------------------------

    {
    position = 0;
    byte[] buffer = new byte[sizeof(Int64)];
    BinaryReader br = new BinaryReader(pStream);
    br.Read(buffer, 0, recordSize);
    position = BitConverter.ToInt64(buffer, 0);
    return true;
    }

    ------------------------------------------------------------------------------

    I would really appreciate if you could please guide me to what is it that can be used in Qt to implement it ?

    Regards,
    Raj

  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: Help with Binary reader in qt ???

    http://doc.qt.nokia.com/4.6/qdatastream.html

    Something like this maybe?

    Qt Code:
    1. QFile file(...);
    2. file.open(QIODevice::ReadOnly);
    3. QDataStream in(&file);
    4. while (!in.atEnd()) {
    5. qint64 a;
    6. in >> a;
    7. // Do something with a
    8. }
    To copy to clipboard, switch view to plain text mode 

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

    rajji_saini (15th September 2010)

  4. #3
    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: Help with Binary reader in qt ???

    I think it is more like this:

    Qt Code:
    1. QFile pStream(...);
    2. pStream.open(...);
    3. QByteArray buffer = pStream.read(sizeof(qint64));
    4. qint64 val;
    5. memcpy(&val, buffer.constData(), sizeOf(qint64));
    To copy to clipboard, switch view to plain text mode 
    Just note this is extremly unportable (which is not the case for the solution suggested by my preposter).
    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.


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

    rajji_saini (15th September 2010)

  6. #4
    Join Date
    Aug 2010
    Posts
    36
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Help with Binary reader in qt ???

    Thanks guyz this helped me alot

  7. #5
    Join Date
    Aug 2010
    Posts
    36
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Help with Binary reader in qt ???

    Hi,

    I have a text file ... and I stored some value like "123" in it.
    I am now trying to read the 3 bytes out of this text file as follows:

    ------------
    QFile fileIn("Test.txt);
    QByteArray buffer = fileIn.read(3);
    qint32 val;
    memcpy(&val,buffer.constData(),3);
    qDebug() << "VAL:" << val;
    qDebug() << "FileSize:" << fileIn->size();
    ------------
    But I am not getting the outpt as 123 ... It gives some garbage value

    FileSize: 3
    VAL: 3355185

    Could you please help ... what is it that I am doing wrong here ???

    regards,
    Raj

  8. #6
    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: Help with Binary reader in qt ???

    The value 3355185 is not garbage, that is the correct number!

    This is what happens:
    You have a byte array containing the bytes (in decimal numbers): 49, 50, 51
    The byte 49 represents the number 1, etc...

    The same bytes in bits are:

    Qt Code:
    1. Number | Byte | Bits
    2. -------+-------+-------------------------
    3. 1 | 49 | 00110001
    4. 2 | 50 | 00110010
    5. 3 | 51 | 00110011
    To copy to clipboard, switch view to plain text mode 

    When you do the memory copy to an int, it will copy the 3 8-bit series into your 32 bit number, resulting in the number:

    1100110011001000110001
    Which is the number:
    3355185


    Do not make it yourself too difficult and use a QTextStream instead of a memory copy.

    Qt Code:
    1. QTextStream stream(&inFile);
    2.  
    3. qint32 number = 0;
    4.  
    5. stream >> number;
    6.  
    7. qDebug() << "number:" << number;
    To copy to clipboard, switch view to plain text mode 

  9. #7
    Join Date
    Aug 2010
    Posts
    36
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Help with Binary reader in qt ???

    Can I use QTextStream in a scenario where I have to read a specific number of bytes from the given data.
    In my example above I mentioned a text file ... How can I implement if I have to lets say read first two bytes out of a binary file ???
    Considering the same example above if I were to read only "1" and store it as an int ... How could I do that ???

    Regards,
    Raj

  10. #8
    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: Help with Binary reader in qt ???

    Consider the following file:

    Qt Code:
    1. This is a text containing the number 1 2 and 1234
    To copy to clipboard, switch view to plain text mode 

    You can use something like this:

    Qt Code:
    1. QTextStream stream(&inFile);
    2.  
    3. QString text = QString("");
    4. qint32 number = 0;
    5. char *bigNumber = "";
    6.  
    7. stream >> text; // text contains the word "This"
    8. stream >> text; // text contains the word "is"
    9. stream >> text; // text contains the word "a"
    10. stream >> text; // text contains the word "text"
    11. stream >> text; // text contains the word "containing"
    12. stream >> text; // text contains the word "the"
    13. stream >> text; // text contains the word "number"
    14. stream >> number; // number = 1
    15. stream >> text; // text contains the word "2"
    16. stream >> text; // text contains the word "and"
    17. stream >> bigNumber; // bigNumber = "1234", bigNumber[0] = "1", etc...
    18. When using bigNumber and a text stream, make sure you double check the sizes!!!
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. RSS Reader
    By numbat in forum Qt-based Software
    Replies: 2
    Last Post: 6th April 2011, 10:42
  2. Runing inline evice/adobe reader
    By deimus in forum Qt Programming
    Replies: 2
    Last Post: 30th June 2010, 07:19
  3. reseting reader
    By uchennaanyanwu in forum Newbie
    Replies: 1
    Last Post: 1st August 2008, 20:28
  4. xml reader
    By mickey in forum General Programming
    Replies: 5
    Last Post: 13th January 2008, 18:37
  5. Qt + Barcode Reader
    By sunil.thaha in forum General Discussion
    Replies: 12
    Last Post: 13th November 2007, 12:13

Tags for this Thread

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.