Convert QbyteArray to qreal
-
Hi
Its impossible to guess for anyone as it really, depends on what is inside the QbyteArray.
If its related to the QCanBusFrame thing, then I suggest trying to dump the payload and see what is inside.
Only you can know.We cannot convert some random amount of bytes to a qreal as it would not be correct.
Does to int give a valid value ? Are you sure ther eeven IS a qreal value send from the other end ?
-
@dziko147 said in Convert QbyteArray to qreal:
5byte .
And the type is real .No. A standard c real value is either 4 or 8 bytes. If you get 5 bytes you have to interpret them by your own based on the spec from your sending device.
-
@mrjj yes its a payload of a Can bus frame .
the can bus send 5byte .
And the type is real .@dziko147 said in Convert QbyteArray to qreal:
@mrjj yes its a payload of a Can bus frame .
the can bus send 5byte .
And the type is real .Great, only 120 possible permutations.
You'll need more information from the sender side.
-
the frame sent is compouned of 1byte of bus status and then 4 byte for the data (the real variable) .
So in this case what should I do ?@dziko147 there are tons of ways,
-
I personally would prefer QDataStream
-
Quick and dirty:
//QByteArray frame; float value{0}; memcpy(&value, frame.data()+1, 4);
50/50 that this would result in the "correct" float
-
-
@dziko147 there are tons of ways,
-
I personally would prefer QDataStream
-
Quick and dirty:
//QByteArray frame; float value{0}; memcpy(&value, frame.data()+1, 4);
50/50 that this would result in the "correct" float
@J-Hilk said in Convert QbyteArray to qreal:
memcpy(&value, frame.data()+1, 4);
OOI, would you really write
4
as you have done for the size? (I admit I know nothing about what a bus-frame is!) -
-
@JonB said in Convert QbyteArray to qreal:
OOI, would you really write 4 as you have done for the size?
Because of 'dirty' :D
-
@dziko147 said in Convert QbyteArray to qreal:
Can you please provide an example with QDataStream :)
It is so hard to read documentation?
//QByteArray frame QDataStream stream(frame); quint8 byte; float value; stream >> byte; // skip first byte stream >> value: // read float value
-
@dziko147 said in Convert QbyteArray to qreal:
Can you please provide an example with QDataStream :)
It is so hard to read documentation?
//QByteArray frame QDataStream stream(frame); quint8 byte; float value; stream >> byte; // skip first byte stream >> value: // read float value
And only one of the two examples will give the 'correct' real value :D
-
@J-Hilk said in Convert QbyteArray to qreal:
memcpy(&value, frame.data()+1, 4);
OOI, would you really write
4
as you have done for the size? (I admit I know nothing about what a bus-frame is!)@JonB you will have noticed, I omitted also any kind of size checks as well :D
@dziko147 ,@KroMignon gave a good example
if you do not care about the status byte, you could also use QDataStream::skipRawData,
just saying :D
-
Currently I test a static QByteArray .
this is my code :
QByteArray rpmdata = "FF10001000100010001000100010001000";
qDebug() << rpmdata << "this is data" ;QDataStream stream(rpmdata);
quint8 byte;
qreal rpmint;
stream >> byte; // skip first byte
stream >> rpmint; // read float value
qDebug() << rpmint << "this is data to int" ;back.setValue(rpmint);
-
Currently I test a static QByteArray .
this is my code :
QByteArray rpmdata = "FF10001000100010001000100010001000";
qDebug() << rpmdata << "this is data" ;QDataStream stream(rpmdata);
quint8 byte;
qreal rpmint;
stream >> byte; // skip first byte
stream >> rpmint; // read float value
qDebug() << rpmint << "this is data to int" ;back.setValue(rpmint);
@dziko147 said in Convert QbyteArray to qreal:
Currently I test a static QByteArray .
this is my code :
QByteArray rpmdata = "FF10001000100010001000100010001000";What do you want to do with this?
If the data are hexadecimal coded, then you have to convert them to binary:
QByteArray rpmdata = QByteArray::fromHex("FF10001000100010001000100010001000");
-
@KroMignon
So I try to receive data from QCanBusFrame .
So I extract the payload using : QByteArray QCanBusFrame::payload() const .
Currently I try to test a static QByteArray (I mean that i write manually the QByteArray) wich is the "rpmdata " .
the type of my value is real .
the frame compouned of 5byte( 1 for status and 4 for data) .
I hope that my problem looks clear . -
@dziko147 said in Convert QbyteArray to qreal:
I hope that my problem looks clear .
No since you still not tell us how the sender encodes the data and what value you expect.
-
@KroMignon
So I try to receive data from QCanBusFrame .
So I extract the payload using : QByteArray QCanBusFrame::payload() const .
Currently I try to test a static QByteArray (I mean that i write manually the QByteArray) wich is the "rpmdata " .
the type of my value is real .
the frame compouned of 5byte( 1 for status and 4 for data) .
I hope that my problem looks clear . -
@KroMignon Exact . I write manually this test data .
@Christian-Ehrlicher I will receive a frame using QCanBusFrame , So the data will be in a QByteArray variable . I expect a Rpm Value wich is real .