Solved Reading Raw Data from File
-
Hi guys, I am trying to read a raw data file as follows:
QFile fileName("C:/0data/QT Projects new/falcon/18071209.422"); fileName.open(QIODevice::ReadOnly); QDataStream in(&fileName); QByteArray line_data(7,0); while (!in.atEnd()) { in.readRawData(line_data.data(),7); v_lap = line_data.mid(0,1); v_data1 = line_data.mid(1,1); v_data2 = line_data.mid(2,1); v_data3 = line_data.mid(3,1); v_data4 = line_data.mid(4,1); v_datacr = line_data.mid(5,1); v_datalf = line_data.mid(6,1); vlap_max = v_lap.toInt(); }
And here are the results:
Variables locales line_data "\001\030\030\000\030\r\n" QByteArray v_data1 "\030" QVariant (QByteArray) v_data2 "\030" QVariant (QByteArray) v_data3 "\000" QVariant (QByteArray) v_data4 "\030" QVariant (QByteArray) v_datacr "\r" QString v_datalf "\n" QString v_lap "\001" QVariant (QByteArray) vlap_max 0 qint16
My concern is about the values who seems to be always seen as char despite a raw_data read and
when trying to convert v_lap to int the result is always 0 !!Thks for your usual coop
JP
-
I finally succeed to read my file in Raw mode with QVariant as follows:
QFile fileName("C:/0data/QT Projects new/datafile");
fileName.open(QIODevice::ReadOnly);QDataStream in(&fileName);
QByteArray record_data(1,0);
while (!in.atEnd()) {
in.readRawData(record_data.data(),1); v_lap=(*record_data.constData()); in.readRawData(record_data.data(),1); v_data1=(*record_data.constData()); in.readRawData(record_data.data(),1); v_data2=(*record_data.constData()); in.readRawData(record_data.data(),1); v_data3=(*record_data.constData()); in.readRawData(record_data.data(),1); v_data4=(*record_data.constData());
}
and the result in the debugger is:
v_data1 48 QVariant (int) v_data2 49 QVariant (int) v_data3 50 QVariant (int) v_data4 51 QVariant (int)
Thks to all of you for the support and sorry to not catch up so quick
JP
-
@jipe3001 See http://doc.qt.io/qt-5/qbytearray.html#toInt
You should rather use QDataStream and do the conversion using its >> operators like http://doc.qt.io/qt-5/qdatastream.html#operator-gt-gt-4 -
@jipe3001 said in Reading Raw Data from File:
thats because v_lap is a QVariant of type (QByteArray) theres most likly no conversion in QVariant that turns a QByteArray into a n int
try the following
v_lap = line_data.mid(0,1).toInt();
thats still with some heavy copy and conversion operation but it should return the correct int.
-
- Why are you using
QVariant
? QVariant::toInt
on aQByteArray
will use string conversion
My concern is about the values who seems to be always seen as char
char
is the only type in the standard assured to havesizeof==1
so it is used to represent a "byte of data"when trying to convert v_lap to int the result is always 0
What you want is
int v_lapInt(*v_lap.toByteArray().constData());
- Why are you using
-
Thks for all your answers but I guess my real problem is about the conversion to INT
For example I am reading the following QByteArray as follows:
QByteArray test_lap(1,0);
in.readRawData(test_lap.data(),0);And I've got this in debuging mode monitor:
test_lap "\001" QByteArray
1 0x01 charTherefore I tried to convert this Hexa value to Int as follows:
bool ok; int x_lap2 = test_lap.toInt(&ok,16);
But the results are always:
ok false bool
x_lap2 0 intWhat is wrong is this convertion which should be so simple?
-
@jipe3001 You should really take a look at http://doc.qt.io/qt-5/qdatastream.html It greatly simplifies reading binary data.
-
@jipe3001 said in Reading Raw Data from File:
test_lap "\001" QByteArray
1 0x01 charIt's not a ascii hex value,.
example:
const char* c="d"; // 13 hex QByteArray arr= QByteArray(c); int v=arr.toInt(0,16); // convert to binary qDebug()<<v; // display 13 in decimal (default)
\001 means 1 ( escape octal value)
const char* c="\011"; // 9 octal QByteArray arr= QByteArray(c); int v=arr.at(0); qDebug()<<v; // display 9 in decimal (default)
-
@jipe3001 said in Reading Raw Data from File:
What is wrong is this convertion which should be so simple?
@VRonin said in Reading Raw Data from File:
toInt
on aQByteArray
will use string conversion
What you want isint v_lapInt(*v_lap.toByteArray().constData());
-
I finally succeed to read my file in Raw mode with QVariant as follows:
QFile fileName("C:/0data/QT Projects new/datafile");
fileName.open(QIODevice::ReadOnly);QDataStream in(&fileName);
QByteArray record_data(1,0);
while (!in.atEnd()) {
in.readRawData(record_data.data(),1); v_lap=(*record_data.constData()); in.readRawData(record_data.data(),1); v_data1=(*record_data.constData()); in.readRawData(record_data.data(),1); v_data2=(*record_data.constData()); in.readRawData(record_data.data(),1); v_data3=(*record_data.constData()); in.readRawData(record_data.data(),1); v_data4=(*record_data.constData());
}
and the result in the debugger is:
v_data1 48 QVariant (int) v_data2 49 QVariant (int) v_data3 50 QVariant (int) v_data4 51 QVariant (int)
Thks to all of you for the support and sorry to not catch up so quick
JP