how to convert text (real number) in LineEdit to an array of bytes in Hexadecimal
-
Hello Qt, How I can convert a real number entered by user in Line-edit to 4-bytes array represented in Hexadecimal, lets say the user enters the speed of DC motor =3564 (RPM), I want to save it as an array of hex this array looks like {(DE C0 00 00)} , where (3564)DC=(DE C0 00 00)HEX.
I tried in this code but it not work correctly.void MainWindow::on_speed_ref2_lineEdit_returnPressed() { float fdata2 = 0.0f; fdata2 = ui->speed_ref2_lineEdit->text().toFloat(); uint8_t *data2 = new uint8_t(); data2 = (uint8_t*)&fdata2; frame2[3] =data2[0]; frame2[4] = data2[1]; frame2[5] = data2[2]; frame2[6] = data2[3]; serial->write((char*)frame2, 4); }
-
@hussam.Yones Hi, and welcome to the Qt forum!
const int input = 3564; const QByteArray ba = QString::number(input, 16).toLatin1(); const char * result = ba.constData(); // pointer remains valid as long as 'ba' is alive
-
@Wieland , Thank you very much it works for input of type integer, but in fact I need more precision I mean I consider the entered value is of type float, (ie float a=3564.0).
-
I'm not sure if what you're doing there makes any sense: In the resulting hex string, how do you know where the decimal point is? Anyways:
const double input = 23.42f; const QStringList list = QString::number(input).split('.'); const int ia = list.at(0).toInt(); const int ib = list.at(1).toInt(); const QByteArray a = QString::number(ia, 16).toLatin1(); const QByteArray b = QString::number(ib, 16).toLatin1(); const QByteArray ba = a + b; const char * result = ba.constData();
-
@Wieland, this float number is represent the speed Value of DC motor in RPM, may you mean I it should be in double type or something else!!.
in fact I don't know obviously whit's the type I should use this is the GUI I built
link text
you can see in Stack-overflow here:
link text
may you get what I need, I'am sory to be annoying but I spent alot of time in this problem :(