Is there a way to read a certain amount of bytes from the serial and type cast it into int or float? I've read through the QByteArray document, but I don't really get it.
Is there a way to read a certain amount of bytes from the serial and type cast it into int or float? I've read through the QByteArray document, but I don't really get it.
Why not simply use QByteArray::toFloat() or toInt()?
What exactly didn't you get?I've read through the QByteArray document, but I don't really get it.
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
I've tried using toFloat(), but kept on getting 0
You're going to have to show some code if you want help in finding what's wrong.
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
i.e.
QByteArray a("425AE78F");
float serial = a.toFloat();
qDebug() << serial;
I'm getting 0 instead of 54.72613. I don't think I'm doing it correctly.
This is example from QByteArray::toDouble doc :Do you see the difference ?Qt Code:
double a = string.toDouble(); // a == 1234.56To copy to clipboard, switch view to plain text mode
Are you saying that the string a is not a float that's why it fails to convert?
Of course. The number of float type and its text representation is two different things. You must go back to the basics of C ++.
As you can tell, I'm still learning c++. If you don't mind, could you give me an example of how does the float conversion works?
"4h\xB2\xC9\xBC'\xB2\xC9<\xBC\xF9K;\xD1\xEB\x7 F?"
The above is a chunk of my code, and I'm trying to convert it to float, but I couldn't get it to work. I really appreciate your help.
How is the data sent? The order of bytes is important.
Actually I'm reading the data from the serial. I have 9 values print out in arduino as double, so I want to read it 36 bytes at a time and convert it to float.
What does it mean "print out in arduino as double" ? You must do the opposite operation to "print out".
Are these the actual bytes you get from the arduino, or have you converted them to a string? This could almost be an array of hexadecimal numbers except for all of the "punctuation / non-hex" characters in it."4h\xB2\xC9\xBC'\xB2\xC9<\xBC\xF9K;\xD1\xEB\x7 F?"
And this is a hexadecimal number, represented as a string literal. You can't use the toDouble() or toFloat() conversions on such as string, because it doesn't represent the string form of a floating point number (ie. "1234.567").QByteArray a("425AE78F");
You can convert this to a long or a long long using something like this:
Qt Code:
long b = a.toLong( 0, 16 );To copy to clipboard, switch view to plain text mode
Last edited by d_stranz; 20th December 2017 at 17:48.
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
[QUOTE=d_stranz;301620]Are these the actual bytes you get from the arduino, or have you converted them to a string? This could almost be an array of hexadecimal numbers except for all of the "punctuation / non-hex" characters in it.
Yes, they are the actual bytes. This is kind of unrelated with the topic, but if I want to send a character 'a' to the serial, can I use serial -> write("a")?
Bookmarks