Convert QbyteArray to qreal
-
@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 . -
@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 .@dziko147 said in Convert QbyteArray to qreal:
. I expect a Rpm Value wich is real .
This tells us nothing.
real
just tells that the value is a floating-point value. It does not tell how this value is encoded and since you can't/won't tell us this you have to find it out by yourself. Two possibilities were already given to you, other encodings are unknown to us and therefore you're on your own when none of them works. -
@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 .@dziko147 said in Convert QbyteArray to qreal:
Exact . I write manually this test data .
I don't know how to say it more clearly:
- What does this represent?
- Is it a hexadecimal representation of your test data?
- CAN frame have a maximum of 8 Byte of payload, this is much more data, so what is it?
You should be able to answer those questions, or you are not knowing what you are doing. Which is very bad.