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!
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!
Those numbers are bits, not bytes.
Ginsengelf
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):
- Use Qt Assistant
- Search the forum
If you haven't found solution yet then create new topic with smart question.
Oh! Im just an idiot!
Thank you for illumination.
Ok. Illumination is good but the code still doesnt work.
Qt Code:
QFile Source_file; Source_file.setFileName("D:/Dela/QTtest/Begin.rg2"); // data.setByteOrder(data.LittleEndian); qint32 intb; data>>intb; qDebug()<<intb; } 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
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:
QFile Source_file; Source_file.setFileName("D:/Dela/QTtest/Begin.rg2"); qint32 intb; QByteArray bar; bar = Source_file.read(4); bool ok; intb = bar.toInt(&ok,0); qDebug()<<intb; } 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.
toInt() does something completely different. What you need to do is something like this:
Qt Code:
qint32 x; memcpy(&x, ba.constData(), 4);To copy to clipboard, switch view to plain text mode
maka (12th May 2009)
It works.
Thank you very much!
Bookmarks