Converting Qbyte array to array and to double
-
wrote on 30 Jul 2021, 13:43 last edited by dziko147
Hello , I get some data from a CAN Bus . So my data is some float and some double .
What I did is :
float myData; double myDatadouble; QDataStream stream(data); qDebug() << data.size() << " Data Data&size" ; if (data.size()== 5){ quint8 byte; stream >> byte; // skip first byte stream.setFloatingPointPrecision(QDataStream::SinglePrecision); stream >> myData; // read data qDebug() << "this is data"<< myData ; QString DatatoString =QString::number(myData); Outputs[i]=DatatoString; qDebug() << "In outputs data" << Outputs[i]; } else { quint8 byte; stream >> byte; // skip first byte stream.setFloatingPointPrecision(QDataStream::DoublePrecision); stream >> myDatadouble; // read data qDebug() << "this is data"<< myDatadouble; QString DatatoString =QString::number(myDatadouble); Outputs[i]=DatatoString; qDebug() << "In outputs data" << Outputs[i]; }
I get only float converted , the double is usually "0" .
thankyou
-
Hello , I get some data from a CAN Bus . So my data is some float and some double .
What I did is :
float myData; double myDatadouble; QDataStream stream(data); qDebug() << data.size() << " Data Data&size" ; if (data.size()== 5){ quint8 byte; stream >> byte; // skip first byte stream.setFloatingPointPrecision(QDataStream::SinglePrecision); stream >> myData; // read data qDebug() << "this is data"<< myData ; QString DatatoString =QString::number(myData); Outputs[i]=DatatoString; qDebug() << "In outputs data" << Outputs[i]; } else { quint8 byte; stream >> byte; // skip first byte stream.setFloatingPointPrecision(QDataStream::DoublePrecision); stream >> myDatadouble; // read data qDebug() << "this is data"<< myDatadouble; QString DatatoString =QString::number(myDatadouble); Outputs[i]=DatatoString; qDebug() << "In outputs data" << Outputs[i]; }
I get only float converted , the double is usually "0" .
thankyou
wrote on 30 Jul 2021, 13:58 last edited by@dziko147 in the else part of the case you do QString::number on the float instead of the double?
Plus, using QString::number you can control precision on the double too https://doc.qt.io/qt-5/qstring.html#number-6
-
wrote on 30 Jul 2021, 14:08 last edited by
@artwaw thanks for your reply . Ok I will remove the Qstring .
Also I get unreadable data like " 3.13891e-43 " for a array "0xff000000e0" .
I use a online converter and give the same value .
But there is a way to make it readable .
thank you
-
@artwaw thanks for your reply . Ok I will remove the Qstring .
Also I get unreadable data like " 3.13891e-43 " for a array "0xff000000e0" .
I use a online converter and give the same value .
But there is a way to make it readable .
thank you
wrote on 30 Jul 2021, 14:20 last edited by@dziko147
again: in the "else" part you haveqDebug() << "this is data"<< myDatadouble; QString DatatoString =QString::number(myData);
where I think you should have
qDebug() << "this is data"<< myDatadouble; QString DatatoString =QString::number(myDatadouble);
since you do not even set
myData
in that part. Hence your string contains rubbish, you don't even initialise the variable. -
Hello , I get some data from a CAN Bus . So my data is some float and some double .
What I did is :
float myData; double myDatadouble; QDataStream stream(data); qDebug() << data.size() << " Data Data&size" ; if (data.size()== 5){ quint8 byte; stream >> byte; // skip first byte stream.setFloatingPointPrecision(QDataStream::SinglePrecision); stream >> myData; // read data qDebug() << "this is data"<< myData ; QString DatatoString =QString::number(myData); Outputs[i]=DatatoString; qDebug() << "In outputs data" << Outputs[i]; } else { quint8 byte; stream >> byte; // skip first byte stream.setFloatingPointPrecision(QDataStream::DoublePrecision); stream >> myDatadouble; // read data qDebug() << "this is data"<< myDatadouble; QString DatatoString =QString::number(myDatadouble); Outputs[i]=DatatoString; qDebug() << "In outputs data" << Outputs[i]; }
I get only float converted , the double is usually "0" .
thankyou
-
wrote on 30 Jul 2021, 14:38 last edited by dziko147
@CP71 said in Converting Qbyte array to array and to double:
8! so doesn't you have a one spare byte in else as in float case?
no I usually get frame started with FF .
I mean than in the case of a float I get "FF + 4Byte data"
In the case of a Double I get "FF +7Byte data "
and here I skip the first byte
quint8 byte; stream >> byte; // skip first byte
-
@CP71 said in Converting Qbyte array to array and to double:
8! so doesn't you have a one spare byte in else as in float case?
no I usually get frame started with FF .
I mean than in the case of a float I get "FF + 4Byte data"
In the case of a Double I get "FF +7Byte data "
and here I skip the first byte
quint8 byte; stream >> byte; // skip first byte
wrote on 30 Jul 2021, 14:41 last edited by CP71@dziko147 said in Converting Qbyte array to array and to double:
In the case of a Double I get "FF +7Byte data "
Sorry, but in double case I think you'd send 0xFF+8bytes, anyway I think your application tries read a double at 8 bytes
-
@CP71 now how can make it readable .
For example I get " 3.13891e-43 " for a array "0xff000000e0" .
pleasewrote on 30 Jul 2021, 15:15 last edited by@dziko147
It is wrong!https://www.binaryconvert.com/convert_double.html
As last time, I must go.
Sorry, bye
4/14