Read binary data from file
-
QDataStream s(&file); s.setByteOrder(QDataStream::LittleEndian); s.skipRawData(80); qint32 value; s >> value; qDebug() << value;
give me the same result, 0...
@HB76 just in case, could you please post:
- the values of the bytes at positions 80, 81, 82, 83 for the file you're working with?
- the size of the data (or the whole file) for such file?
-
I've found a way to do it :
QByteArray data = file.read(4); qint32 facet_count; memcpy(&facet_count, data.constData(), 4); qDebug() << facet_count;
Just for knowledge, why this simple code doesn'y work ?
file.seek(80); ulong length = file.read(4).toULong(); qDebug() << length;
it would be much more simplier..
-
@HB76 said in Read binary data from file:
ulong length = file.read(4).toULong();
Because you did not read the documentation: https://doc.qt.io/qt-5/qbytearray.html#toLong
-
@HB76 said in Read binary data from file:
ulong length = file.read(4).toULong();
Because you did not read the documentation: https://doc.qt.io/qt-5/qbytearray.html#toLong
@Christian-Ehrlicher I was just having a look at it but I supposed I didn't understand the whole explaination the first time as I'm not native english ^^'
-
The example there should be obvious:
QByteArray str("FF"); bool ok; int hex = str.toInt(&ok, 16); // hex == 255, ok == true int dec = str.toInt(&ok, 10); // dec == 0, ok == false
As you can see it converts a string value into an integer
-
@HB76 said in Read binary data from file:
ut why every time the conversion failed with this method whereas the conversion with memcpy is working ?
Again: toUint() interprets your string as ascii text and tries to convert it to an integer, whereas memcpy simply copies the plain data - C basics on how a value is interpreted.