Results 1 to 8 of 8

Thread: reading bytes out of a file

  1. #1
    Join Date
    Feb 2009
    Posts
    16
    Thanks
    2
    Platforms
    Unix/X11

    Default reading bytes out of a file

    Hello there

    I am trying to parse a file by reading the bytes from it (ideally hex).

    But I have a snag...

    any bytes over the value of 127 give me problems (i.e. -128 etc).

    How can i read the bytes out of the array.

    for example. i have 2 qbytearrays here:

    Qt Code:
    1. count = 6;
    2. arrayParse = array.mid(b,2);
    To copy to clipboard, switch view to plain text mode 

    (the data contained within these two bytes is b00d)

    if i try to read the first byte, i get a negative number.

    how can i read it so i get the correct value?

    or if that fails, how can i just read the file as hex??

    (sorry if this is a chumps question!)

    and in case you need more info, i have opened the file using file.open(QIODevice::ReadOnly);

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: reading bytes out of a file

    Please try to give a self contained example. Your 2 lines are not understandable - lacking context etc.

    A char is (usually) signed and has (integer) values from -128 to +127. Use unsigned char if you want 0 to 255.

    And apropos QByteArrays: how did you write those to that file?

  3. #3
    Join Date
    Oct 2009
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: reading bytes out of a file

    If you know what type of data is in the file then read it as the correct type rather than as an array of chars, which is what I assume you are doing.

    Do something like this:

    Qt Code:
    1. QFile file(fileName);
    2. if (!file.open(QIODevice::ReadOnly)) {
    3. // out put some debug
    4. return;
    5. }
    6.  
    7. QDataStream in(&file);
    8. in.setByteOrder(QDataStream::LittleEndian); // or what ever order determined from where originating machine type (or how written)
    9. in.skipRawData(2); // skip some data
    10. qint16 val1;
    11. in >> val1;
    12. // continue reading appropriate types from the input
    To copy to clipboard, switch view to plain text mode 

    usually will have a check for end of file e.g.: while(!in.atEnd())

    Hope this is clear enough

  4. #4
    Join Date
    Feb 2009
    Posts
    16
    Thanks
    2
    Platforms
    Unix/X11

    Default Re: reading bytes out of a file

    thanks for you input.

    i will try to give some more information.

    I am trying to analyse the data contained within a hex file.

    so for example:

    Qt Code:
    1. array = file.readAll();
    2. int numBytes = array.length();
    3.  
    4. if (numBytes > 0 )
    5. {
    6. a = 0; // counter for position in the array
    7.  
    8. while (( a < numBytes ) && (data_parsed == 0))
    9. {
    10. long sequenceCode = 0;
    11.  
    12. a = array.indexOf(71, a); // looking for hex 47
    13. std::cout << "position of first 47 hex byte = " << a << std::endl;
    14.  
    15. arrayReadBytes = array.mid(a,3);
    16. b = a+3;
    17.  
    18. ...
    To copy to clipboard, switch view to plain text mode 

    so basically i need to search the required file for certain bytes and then act on them.

    Then later on, I am checking the following bytes:

    Qt Code:
    1. QByteArray array2;
    2. array2 = array.mid(b,2);
    To copy to clipboard, switch view to plain text mode 

    its at this point that I fall down, as i mentioned before, i am overflowing.

    As for using QDatastream, I have checked the documentation, and I can't seem to see the search functionality that i had from using a qbytearray....

  5. #5
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: reading bytes out of a file

    ok, and (disregarding such things as endianness etc...) doesn't
    Qt Code:
    1. QDataStream ds(array2);
    2. qint16 i;
    3. ds >> i;
    To copy to clipboard, switch view to plain text mode 
    work for you?

    (If you are on the same machine that wrote the file, maybe something like
    Qt Code:
    1. qint16 i = *reinterpret_cast<const qint16*>(array.constData()+b)
    To copy to clipboard, switch view to plain text mode 
    will work, too.)

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

    priceey (6th October 2009)

  7. #6
    Join Date
    Feb 2009
    Posts
    16
    Thanks
    2
    Platforms
    Unix/X11

    Default Re: reading bytes out of a file

    I managed to get some information out, using your idea

    qint16 i = *reinterpret_cast<const qint16*>(array.constData()+b)

    but changed it to qint8 to read 1 byte instead (i don't always want to use 2 bytes)

    hopefully i can get on with it now!

  8. #7
    Join Date
    Feb 2009
    Posts
    16
    Thanks
    2
    Platforms
    Unix/X11

    Default Re: reading bytes out of a file

    while i am still here..

    is there an easy way to keep the leading zeros in the number???

    i.e.

    value in the file = 0d

    after reading out the value = d

  9. #8
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: reading bytes out of a file

    leading zeros... that is not a question of reading data, but of output formatting.
    How do you print the value so far? Are you aware of QString::arg()? There are overloads that allow you to specify fieldwidth, base and fill-character.

    HTH

Similar Threads

  1. Reading and rereading a file with QXmlStreamReader
    By TheRonin in forum Qt Programming
    Replies: 14
    Last Post: 30th April 2015, 14:04
  2. Reading and writing bytes in file
    By DiamonDogX in forum Qt Programming
    Replies: 2
    Last Post: 20th May 2009, 20:25
  3. File Reading Advice
    By tntcoda in forum Qt Programming
    Replies: 1
    Last Post: 11th November 2008, 19:44
  4. QTextStream loses position while reading file
    By bjh in forum Qt Programming
    Replies: 2
    Last Post: 13th February 2008, 15:47
  5. QTcpSocket exception.
    By Fastman in forum Qt Programming
    Replies: 9
    Last Post: 29th January 2008, 13:51

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.