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. convert HEX to double
Forum Updated to NodeBB v4.3 + New Features

convert HEX to double

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 5 Posters 4.2k Views 2 Watching
  • 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.
  • R Offline
    R Offline
    rezaMSLM
    wrote on last edited by
    #1

    I want to convert LineEdit contents(HEX value) to double and this is my code:

    void settings::on_pb2_clicked()
    {
        QString s = QString::number(ui->lineEdit_Temp1_Read->text().toDouble());
        ui->lineEdit_Temp1_Read->setText(s);
        qDebug()<< s;
    
    }
    

    but if the LineEdit contents include characters like A, B, C, D, E, F the content is changed to zero when click on pb2

    how can I convert it's contents to double?

    aha_1980A 1 Reply Last reply
    0
    • R rezaMSLM

      I want to convert LineEdit contents(HEX value) to double and this is my code:

      void settings::on_pb2_clicked()
      {
          QString s = QString::number(ui->lineEdit_Temp1_Read->text().toDouble());
          ui->lineEdit_Temp1_Read->setText(s);
          qDebug()<< s;
      
      }
      

      but if the LineEdit contents include characters like A, B, C, D, E, F the content is changed to zero when click on pb2

      how can I convert it's contents to double?

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @rezaMSLM please give an example input and the expected output.

      Qt has to stay free or it will die.

      R 1 Reply Last reply
      0
      • G Offline
        G Offline
        Gerd
        wrote on last edited by
        #3

        You need to use a conversion function with hex-support, not available for doubles...
        So, you have to

        • convert the String to int/long/longlong with
          e.g. QString::.toInt(bool *ok = NULLPTR, int base = 10) called with base =16
        • convert the resulting value to double if ok is true

        Anyway, setting the resulting value back into your lineEdit seems not to be the best idea...

        1 Reply Last reply
        4
        • aha_1980A aha_1980

          @rezaMSLM please give an example input and the expected output.

          R Offline
          R Offline
          rezaMSLM
          wrote on last edited by
          #4

          @aha_1980
          alt text

          is there any method in qt to do this?

          aha_1980A raven-worxR JKSHJ 4 Replies Last reply
          0
          • R rezaMSLM

            @aha_1980
            alt text

            is there any method in qt to do this?

            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @rezaMSLM so in principle the same as in https://forum.qt.io/topic/74412/qbytearray-to-float-value

            You just need to convert the hex string toUInt() first, with the base 16.

            Qt has to stay free or it will die.

            1 Reply Last reply
            4
            • R rezaMSLM

              @aha_1980
              alt text

              is there any method in qt to do this?

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              @rezaMSLM
              have you tried the following?

              QByteArray::fromHex(hex).toDouble()
              

              i haven't tested this though.

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              aha_1980A 1 Reply Last reply
              0
              • raven-worxR raven-worx

                @rezaMSLM
                have you tried the following?

                QByteArray::fromHex(hex).toDouble()
                

                i haven't tested this though.

                aha_1980A Offline
                aha_1980A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @raven-worx That will not work, as toDouble() expects the data to be in ASCII, e.g. "1.234E+5".

                Qt has to stay free or it will die.

                raven-worxR 1 Reply Last reply
                0
                • aha_1980A aha_1980

                  @raven-worx That will not work, as toDouble() expects the data to be in ASCII, e.g. "1.234E+5".

                  raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by
                  #8

                  @aha_1980
                  right, my fault.
                  This method rather does string parsing.

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  1 Reply Last reply
                  0
                  • R rezaMSLM

                    @aha_1980
                    alt text

                    is there any method in qt to do this?

                    aha_1980A Offline
                    aha_1980A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @rezaMSLM

                    I've uploaded a full example to my Github. Enjoy :)

                    Qt has to stay free or it will die.

                    1 Reply Last reply
                    3
                    • R rezaMSLM

                      @aha_1980
                      alt text

                      is there any method in qt to do this?

                      JKSHJ Offline
                      JKSHJ Offline
                      JKSH
                      Moderators
                      wrote on last edited by
                      #10

                      @rezaMSLM said in convert HEX to double:

                      @aha_1980
                      alt text

                      is there any method in qt to do this?

                      If the hex sequence matches your computer's endianness, then you can convert the hex string to the corresponding bytes and tell your CPU to interpret the bytes as a double value:

                      QByteArray hexStr = ui->lineEdit_Temp1_Read->text().toUtf8();
                      QByteArray bytes = QByteArray::fromHex(hexStr);
                      double *ptr = reinterpret_cast<double*>( bytes.data() );
                      double value = *ptr;
                      

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      1 Reply Last reply
                      2

                      • Login

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