Qt Convert Float to ByteArray
-
Hi,
I used this code for convert float to "ByteArray".
The following codes have different results.
Please guide me.
ThanksCode 1 (It's Not True):
float speed=20; float calcSpeed= funcCalcSpeed(speed); qDebug()<<"calcSpeed: "; qDebug()<<calcSpeed;
Output:
calcSpeed: 41.6667
QByteArray byteArray = QByteArray::fromRawData(reinterpret_cast<char *>(&calcSpeed), sizeof(float)); qDebug()<<"byteArray: "; qDebug()<<byteArray;
Output:
byteArray: "`\xBF\x96\x02"
Code 2 (It's True):
float val=41.6667f; QByteArray byteArray = QByteArray::fromRawData(reinterpret_cast<char *>(&val), sizeof(float)); qDebug()<<"byteArray: "; qDebug()<<byteArray;
Output:
byteArray: "\xB3\xAA&B"
-
@neda said in Qt Convert Float to ByteArray:
41.6667
Just because your float number has 6 same digits it does not mean it's exactly the same value. That's also the reason why you should not compare two floating point numbers (when they are calculated by different algorithms). See also https://en.wikipedia.org/wiki/Floating-point_arithmetic
-
Not sure what you are trying to achieve (hint: when posting a thread, also wrinte some question, or at least some "expected result"), but QByteArray has 2 methods to help you work with floats:
Using setNum
const float number = 20; QByteArray array; array.setNum(number);
Using number
const float number = 20; const QByteArray array = QByteArray::number(number);
-
Not sure what you are trying to achieve (hint: when posting a thread, also wrinte some question, or at least some "expected result"), but QByteArray has 2 methods to help you work with floats:
Using setNum
const float number = 20; QByteArray array; array.setNum(number);
Using number
const float number = 20; const QByteArray array = QByteArray::number(number);
@sierdzio said in Qt Convert Float to ByteArray:
Not sure what you are trying to achieve (hint: when posting a thread, also wrinte some question, or at least some "expected result"),
Thanks for your reply.
I want edit "Code 1" and have output "Code 2".
Output of "code 2" is true.
But instead of "float val=41.6667f;" I have "funcCalcSpeed(speed)".Code 2 (It's True):
float val=41.6667f; QByteArray byteArray = QByteArray::fromRawData(reinterpret_cast<char *>(&val), sizeof(float)); qDebug()<<"byteArray: "; qDebug()<<byteArray;
Output:
byteArray: "\xB3\xAA&B"
I tried"setNum" and "number", but they did not have the desired output (Like output of code 2).
-
I still don't get your problem... did you understand what I wrote above?
-
@sierdzio said in Qt Convert Float to ByteArray:
Not sure what you are trying to achieve (hint: when posting a thread, also wrinte some question, or at least some "expected result"),
Thanks for your reply.
I want edit "Code 1" and have output "Code 2".
Output of "code 2" is true.
But instead of "float val=41.6667f;" I have "funcCalcSpeed(speed)".Code 2 (It's True):
float val=41.6667f; QByteArray byteArray = QByteArray::fromRawData(reinterpret_cast<char *>(&val), sizeof(float)); qDebug()<<"byteArray: "; qDebug()<<byteArray;
Output:
byteArray: "\xB3\xAA&B"
I tried"setNum" and "number", but they did not have the desired output (Like output of code 2).
This post is deleted! -
What prevents you from combining both approaches?
const float speed=20; coinst float calcSpeed= funcCalcSpeed(speed); const QByteArray byteArray = QByteArray::fromRawData(reinterpret_cast<char *>(&calcSpeed), sizeof(float)); qDebug()<<"calcSpeed: "; qDebug()<<byteArray;
(yeah I think I still don't fully get what you're trying to achieve).
-
Hi,
I used this code for convert float to "ByteArray".
The following codes have different results.
Please guide me.
ThanksCode 1 (It's Not True):
float speed=20; float calcSpeed= funcCalcSpeed(speed); qDebug()<<"calcSpeed: "; qDebug()<<calcSpeed;
Output:
calcSpeed: 41.6667
QByteArray byteArray = QByteArray::fromRawData(reinterpret_cast<char *>(&calcSpeed), sizeof(float)); qDebug()<<"byteArray: "; qDebug()<<byteArray;
Output:
byteArray: "`\xBF\x96\x02"
Code 2 (It's True):
float val=41.6667f; QByteArray byteArray = QByteArray::fromRawData(reinterpret_cast<char *>(&val), sizeof(float)); qDebug()<<"byteArray: "; qDebug()<<byteArray;
Output:
byteArray: "\xB3\xAA&B"
Your problem is not converting the float to a sequence of bytes, but that you assume (erroneously) that 2 floating points are the same if they match up to the 4th decimal digit. You need to read and understand what floating points are and why this assumption can almost never be fulfilled.
(See @Christian-Ehrlicher's link with the wiki article)
-
@Christian-Ehrlicher
@sierdzio
@kshegunovThanks a lot
I understood what is happened and my problem has solved.