Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to convert text (real number) in LineEdit to an array of bytes in Hexadecimal

how to convert text (real number) in LineEdit to an array of bytes in Hexadecimal

Scheduled Pinned Locked Moved Unsolved General and Desktop
guiserial interfacserial port
5 Posts 2 Posters 3.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hussam.Yones
    wrote on 22 May 2016, 17:04 last edited by hussam.Yones
    #1

    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);
    
        }
    
    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on 22 May 2016, 17:14 last edited by
      #2

      @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
      
      1 Reply Last reply
      2
      • H Offline
        H Offline
        hussam.Yones
        wrote on 22 May 2016, 17:40 last edited by
        #3

        @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).

        ? 1 Reply Last reply 22 May 2016, 18:02
        0
        • H hussam.Yones
          22 May 2016, 17:40

          @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).

          ? Offline
          ? Offline
          A Former User
          wrote on 22 May 2016, 18:02 last edited by
          #4

          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();
          
          1 Reply Last reply
          1
          • H Offline
            H Offline
            hussam.Yones
            wrote on 22 May 2016, 18:37 last edited by hussam.Yones
            #5

            @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 :(

            1 Reply Last reply
            0

            5/5

            22 May 2016, 18:37

            • Login

            • Login or register to search.
            5 out of 5
            • First post
              5/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved