View Full Version : reading bytes out of a file

5th October 2009, 17:53
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:

count = 6;
arrayParse = array.mid(b,2);

(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);

5th October 2009, 18:17
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?

5th October 2009, 18:22
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:

QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) {
// out put some debug

QDataStream in(&file);
in.setByteOrder(QDataStream::LittleEndian); // or what ever order determined from where originating machine type (or how written)
in.skipRawData(2); // skip some data
qint16 val1;
in >> val1;
// continue reading appropriate types from the input

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

Hope this is clear enough

6th October 2009, 09:14
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:

array = file.readAll();
int numBytes = array.length();

if (numBytes > 0 )
a = 0; // counter for position in the array

while (( a < numBytes ) && (data_parsed == 0))
long sequenceCode = 0;

a = array.indexOf(71, a); // looking for hex 47
std::cout << "position of first 47 hex byte = " << a << std::endl;

arrayReadBytes = array.mid(a,3);
b = a+3;


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:

QByteArray array2;
array2 = array.mid(b,2);

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....

6th October 2009, 10:31
ok, and (disregarding such things as endianness etc...) doesn't

QDataStream ds(array2);
qint16 i;
ds >> i;
work for you?

(If you are on the same machine that wrote the file, maybe something like

qint16 i = *reinterpret_cast<const qint16*>(array.constData()+b)
will work, too.)

6th October 2009, 13:46
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!

6th October 2009, 16:10
while i am still here..

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


value in the file = 0d

after reading out the value = d

6th October 2009, 17:55
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.