Convert QbyteArray to qreal
-
@Christian-Ehrlicher I do not know how the sender encodes the data .
What I know is I receive data using the QCanBusFrame . then I extract the data .
So i get a QByteArray Data . I want to convert it .@dziko147 said in Convert QbyteArray to qreal:
@Christian-Ehrlicher I do not know how the sender encodes the data .
If you do not know how the data is encoded/sent, @Christian-Ehrlicher does not....
-
@KroMignon
I get value =0 after converting :/ -
@Christian-Ehrlicher I do not know how the sender encodes the data .
What I know is I receive data using the QCanBusFrame . then I extract the data .
So i get a QByteArray Data . I want to convert it .@dziko147 said in Convert QbyteArray to qreal:
I do not know how the sender encodes the data
Then how do you want to decode it?
You should at least get some data from sender where you know what exact numbers the sender sent, then you can try to find out how the data was encoded. -
@KroMignon
I get value =0 after converting :/@dziko147
After converting.... what? how?Tell you what: tell us two things:
- Forget about converting anything to float. Show us what the values of the first 5 bytes you receive are. (You say the first byte is a status byte and the next 4 bytes are the value you are interested in, right?)
- Tell us what floating point number you know that encodes/conveys/sends. (Please make sure it is not 0!)
-
@KroMignon
I get value =0 after converting :/ -
@dziko147 said in Convert QbyteArray to qreal:
I get value =0 after converting :/
After converting what?
Please, take time to understand what are the data you want to convert.
I cannot do this for you!@KroMignon
QByteArray rpmdata = QByteArray::fromHex("0102030405"); ---> I want to
convert this .
qDebug() << rpmdata << "this is data" ;
QDataStream stream(rpmdata);
quint8 byte;
float rpmfloat;
stream >> byte; // skip first byte
stream >> rpmfloat; // read float value
qDebug() << rpmfloat << "this is data to float" ;
back.setValue(rpmfloat); ----> I display the value converted here . -
@KroMignon
QByteArray rpmdata = QByteArray::fromHex("0102030405"); ---> I want to
convert this .
qDebug() << rpmdata << "this is data" ;
QDataStream stream(rpmdata);
quint8 byte;
float rpmfloat;
stream >> byte; // skip first byte
stream >> rpmfloat; // read float value
qDebug() << rpmfloat << "this is data to float" ;
back.setValue(rpmfloat); ----> I display the value converted here .@dziko147
You push raw bytes into aQDataStream
. You then try to read that back as, say, a float.Unless I have misunderstood
QDataStream
you cannot do that. It is a structured stream. E.g. it will (well, should) contain "markers" for the data and its types.Experts, I am right here, aren't I (have never used
QDataStream
)?UPDATE
For anyone reading this, the expert answers below indicate this is not the case, and my understanding was incorrect. -
@dziko147
You push raw bytes into aQDataStream
. You then try to read that back as, say, a float.Unless I have misunderstood
QDataStream
you cannot do that. It is a structured stream. E.g. it will (well, should) contain "markers" for the data and its types.Experts, I am right here, aren't I (have never used
QDataStream
)?UPDATE
For anyone reading this, the expert answers below indicate this is not the case, and my understanding was incorrect.@JonB said in Convert QbyteArray to qreal:
E.g. it will (well, should) contain "markers" for the data and its types.
Not for PODs. For QString e.g. a length field is added. What is added for which build-in Qt type is not documented - QDataStream is not meant to be used with anything else than a QDataStream.
-
@dziko147
You push raw bytes into aQDataStream
. You then try to read that back as, say, a float.Unless I have misunderstood
QDataStream
you cannot do that. It is a structured stream. E.g. it will (well, should) contain "markers" for the data and its types.Experts, I am right here, aren't I (have never used
QDataStream
)?UPDATE
For anyone reading this, the expert answers below indicate this is not the case, and my understanding was incorrect.@JonB said in Convert QbyteArray to qreal:
Unless I have misunderstood QDataStream you cannot do that. It is a structured stream. E.g. it will (well, should) contain "markers" for the data and its types.
Of course this is possible, this is no "marker" for serializing/deserializing "base types" like float, double, q(u)int8, q(u)int16, etc.
But "02030405" converted to float is 9.6255135462533111609068148127E-38... almost 0 ;)
-
@KroMignon
QByteArray rpmdata = QByteArray::fromHex("0102030405"); ---> I want to
convert this .
qDebug() << rpmdata << "this is data" ;
QDataStream stream(rpmdata);
quint8 byte;
float rpmfloat;
stream >> byte; // skip first byte
stream >> rpmfloat; // read float value
qDebug() << rpmfloat << "this is data to float" ;
back.setValue(rpmfloat); ----> I display the value converted here .@dziko147 said in Convert QbyteArray to qreal:
QByteArray rpmdata = QByteArray::fromHex("0102030405"); ---> I want to
convert this .What did you expect?
Again, you have to know what you are doing.Here you are converting this array of bytes { 0x02, 0x03, 0x04, 0x05 } to float.
which is 9.6255135462533111609068148127E-38, almost 0.If you change the array to something else like "014F030405", which means converting { 0x4f, 0x03, 0x04 , 0x05} to float, you will obtain 2.19807872E9 (almost 2.2 Billion)
-
@dziko147 said in Convert QbyteArray to qreal:
QByteArray rpmdata = QByteArray::fromHex("0102030405"); ---> I want to
convert this .What did you expect?
Again, you have to know what you are doing.Here you are converting this array of bytes { 0x02, 0x03, 0x04, 0x05 } to float.
which is 9.6255135462533111609068148127E-38, almost 0.If you change the array to something else like "014F030405", which means converting { 0x4f, 0x03, 0x04 , 0x05} to float, you will obtain 2.19807872E9 (almost 2.2 Billion)
@KroMignon I tried this :
QByteArray rpmdata;
rpmdata.resize(4);
rpmdata[0] = 0x43;
rpmdata[1] = 0x96;
rpmdata[2] = 0x7D;
rpmdata[3] = 0xA6;
qDebug() << rpmdata << "this is data" ;QDataStream stream(rpmdata);
float value;
stream >> value; // read float value
qDebug() << value << "this is data to float" ;back.setValue(value);
I get :
the 43967DA6 = (300.98163 ) .
this is the the variable -
@KroMignon I tried this :
QByteArray rpmdata;
rpmdata.resize(4);
rpmdata[0] = 0x43;
rpmdata[1] = 0x96;
rpmdata[2] = 0x7D;
rpmdata[3] = 0xA6;
qDebug() << rpmdata << "this is data" ;QDataStream stream(rpmdata);
float value;
stream >> value; // read float value
qDebug() << value << "this is data to float" ;back.setValue(value);
I get :
the 43967DA6 = (300.98163 ) .
this is the the variable@dziko147 said in Convert QbyteArray to qreal:
the 43967DA6 = (300.98163 ) .
Sorry, my bad, it seems there are something I had forgot about
QDataStream
: when using float you have to specify floating point precision withstream.setFloatingPointPrecision(QDataStream::SinglePrecision);
.The other way is to use(WRONG)qfloat16
QByteArray rpmdata; rpmdata.resize(4); rpmdata[0] = 0x43; rpmdata[1] = 0x96; rpmdata[2] = 0x7D; rpmdata[3] = 0xA6; qDebug() << rpmdata << "this is data" ; QDataStream stream(rpmdata); stream.setFloatingPointPrecision(QDataStream::SinglePrecision); float value; stream >> value; // read float value qDebug() << value << "this is data to float" ; back.setValue(value);
-
@KroMignon thank you it works :) .