Convert QbyteArray to qreal
-
@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.
-
@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 . -
@KroMignon Can you give me an example of a QByteArray compouned of 5bytes ?
-
@KroMignon Can you give me an example of a QByteArray compouned of 5bytes ?
@dziko147 said in Convert QbyteArray to qreal:
Can you give me an example of a QByteArray compouned of 5bytes ?
Yes, here are many examples:
QByteArray b1("ABCDE"); QByteArray b2; b2.resize(5); b2[0] = 0x3c; b2[1] = 0xb8; b2[2] = 0x64; b2[3] = 0x18; b3[4] = 0xca; QByteArray b3 = QByteArray:fromHex("0102030405");
-
@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!)