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:
Code:
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);
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?
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:
Code:
// out put some debug
return;
}
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
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:
Code:
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:
Code:
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....
Re: reading bytes out of a file
ok, and (disregarding such things as endianness etc...) doesn't
work for you?
(If you are on the same machine that wrote the file, maybe something like
Code:
qint16 i = *reinterpret_cast<const qint16*>(array.constData()+b)
will work, too.)
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!
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
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