converting from float or double to QString results in output being 0
-
@JonB Hi and Hi @Kent-Dorfman the amplification_multiplier is #defined at the top of the code I posted
output for the debugs you mentioned
Topic: "Light_Level" , Msg: "5.73628\u0000" test value is "5.73628\u0000" Qstring as float "5.73628\u0000" before amp 0 amp multiplier 10 msg as qstring to double 0 msg as qstring conv "0"
And hi @jsulm how would I go about being able to do this?
Thanks, Dean
-
@Dean21 said in converting from float or double to QString results in output being 0:
msg as qstring to double 0
So that is the only thing you need to look at.
And hi @jsulm how would I go about being able to do this?
Look in
QString
docs to see how you can remove the last character? -
so what is (float)5.7 / (int)10?
Therein is the reason you are getting zero!
-
I question whether removing non-numeric char at end of QSTring is necessary for the numeric conversion problem since all the conversion code I'm familiar with scans digits until it finds an char that can't be part of the number, and then returns everything up to that point.
Not saying it doesn't need to be addressed, but I don't see it as the reason the problem exists.
-
@Kent-Dorfman I did try #define amplfication_multiplier 10.0 instead of #define amplification_multiplier 10, but it made no difference to the final output
-
@jsulm I tried below is the line of code I used, if I used the line you mentioned i get a load of different errors so I removed the QChar part
msg_as_Qstring.remove(("[\u0000]."));
but it still has it attached on the end here is the output
Topic: "Light_Level" , Msg: "5.73628\u0000" test value is "5.73628\u0000" Qstring as float "5.73628\u0000" before amp 0 amp multiplier 10 msg as qstring to double 0 msg as qstring conv "0"
-
@Dean21 said in converting from float or double to QString results in output being 0:
msg_as_Qstring.remove(("[\u0000]."));
How/what do you expect it to match with that
.
at the end?Just chop off the last character, no regular expression, no? Did you find void QString::chop(qsizetype n) ?
If you want to find out whether this will make any difference try
msg_as_Qstring = "5.73628";
first and then you will know!
-
@Kent-Dorfman said in converting from float or double to QString results in output being 0:
so what is (float)5.7 / (int)10?
Umm, that would be
0.57
(not0
).... ! Dividing afloat
by anint
does not convert toint
! :) -
I stand corrected. I was certain the type of the denominator governed the result. Too much time in embedded kernel domain. D'Oh!
-
@Dean21 said in converting from float or double to QString results in output being 0:
msg_as_Qstring.remove(("[\u0000]."));
Why did you put [,] and dot there?
I suggested this:msg_as_Qstring.remove(QChar("\u0000"));
-
if in fact the conversion is being broken by the unicode null terminator then consider an intermediate conversion thru (char*) instead.
QString num(byteArray.data());
since char arrays have no defined length the constructor shoudl use the null as a string terminator, not as part of the string (as is the case when converting from a container that has an internal size field).
-
@Dean21
Glad it's working for you now. That's the steps I would have taken to get it working.@Kent-Dorfman said earlier
I question whether removing non-numeric char at end of QSTring is necessary for the numeric conversion problem since all the conversion code I'm familiar with scans digits until it finds an char that can't be part of the number, and then returns everything up to that point.
But, for whatever reason, conversions to numeric from
QString
state:Warning: The
QString
content may only contain valid numerical characters which includes the plus/minus sign, the character e used in scientific notation, and the decimal point. Including the unit or additional characters leads to a conversion error.So that's how they do it --- all characters in the string must be valid. You could use:
bool ok; double val = msg_as_Qstring.toDouble(&ok); if (!ok) qDebug() << "Conversion failed!";
-
@JonB said in converting from float or double to QString results in output being 0:
Warning: The QString content may only contain valid numerical characters which includes the plus/minus sign, the character e used in scientific notation, and the decimal point. Including the unit or additional characters leads to a conversion error.
In which case RTFM is the rule of the day! but I'm lazy.