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 from int to hex
Forum Updated to NodeBB v4.3 + New Features

Convert from int to hex

Scheduled Pinned Locked Moved General and Desktop
11 Posts 8 Posters 79.2k Views 1 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.
  • C Offline
    C Offline
    clouca
    wrote on 1 Jul 2013, 09:15 last edited by
    #1

    Hello guys i have a problem in converting from int to hex. For example ..
    I have the number 203888026 which conversion is 0C27159A. I want this 0 in front of the conversion because i have to do a comparison from a value a take from serial port.
    Thanks.
    My code is
    @uint decimal = 203888026;
    QString hexadecimal;
    hexadecimal.setNum(decimal,16); @

    The result is
    hexadecimal = C27159A without the zero.
    how can i make the conversion with the 0 ?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on 1 Jul 2013, 09:22 last edited by
      #2

      why do you want to compare the two strings instead of two numbers?

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sierdzio
        Moderators
        wrote on 1 Jul 2013, 09:27 last edited by
        #3

        Try with QByteArray::fromHex(). As far as I know, it prepends that 0.
        @
        hexadecimal = QByteArray::fromHex(QString::number(decimal));
        @

        Or add 0 yourself :) It needs to be added there only when number of digits is uneven.

        (Z(:^

        1 Reply Last reply
        0
        • C Offline
          C Offline
          clouca
          wrote on 1 Jul 2013, 09:28 last edited by
          #4

          There is a reason but i can't starting explaining the whole story.. but i want to compare two hex string.. that's it.

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            qxoz
            wrote on 2 Jul 2013, 04:51 last edited by
            #5

            As i understand you have hext string, and you want compare it with some number. Thats right? In this case better convert string to number and than compare two numbes. But if you realy want convert decimal to string you can add leading zero manualy.
            @uint decimal = 203888026;
            QString hexadecimal;
            hexadecimal.setNum(decimal,16);
            if(hexadecimal.length() %2)hexadecimal.insert(0,QLatin1String("0"));@

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dbzhang800
              wrote on 2 Jul 2013, 05:22 last edited by
              #6

              Add if you want to compare a hex-string with the hex-string you received from serial port, you had better convert both to lower case or upper case.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mp035
                wrote on 20 Nov 2014, 06:29 last edited by SGaist
                #7

                I stumbled across this thread and was very disappointed that no one actually answered the OP's question, unless you count discrediting requirements as an answer. :-)

                For anyone else looking for an answer, QString::arg is exactly what you are after for this sort of conversion, because it has a "base", "fieldwidth" and a "fillchar" field:

                QString QString::arg ( int a, int fieldWidth = 0, int base = 10, const QChar & fillChar = QLatin1Char( ' ' ) ) const

                example:

                uint decimal = 203888026;
                QString hexvalue = QString("%1").arg(decimal, 8, 16, QLatin1Char( '0' ));
                

                you can even add a preceding "0x" if you desire, like this:

                QString hexvalue = QString("0x%1").arg(decimal, 8, 16, QLatin1Char( '0' ));
                
                G N 2 Replies Last reply 21 Sept 2015, 05:02
                15
                • J Offline
                  J Offline
                  JKSH
                  Moderators
                  wrote on 20 Nov 2014, 07:44 last edited by
                  #8

                  Hi mp035,

                  Welcome to the Qt Dev Net, and thank you for posting a solution!

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    JKSH
                    Moderators
                    wrote on 20 Nov 2014, 07:44 last edited by
                    #9
                    This post is deleted!
                    1 Reply Last reply
                    0
                    • M mp035
                      20 Nov 2014, 06:29

                      I stumbled across this thread and was very disappointed that no one actually answered the OP's question, unless you count discrediting requirements as an answer. :-)

                      For anyone else looking for an answer, QString::arg is exactly what you are after for this sort of conversion, because it has a "base", "fieldwidth" and a "fillchar" field:

                      QString QString::arg ( int a, int fieldWidth = 0, int base = 10, const QChar & fillChar = QLatin1Char( ' ' ) ) const

                      example:

                      uint decimal = 203888026;
                      QString hexvalue = QString("%1").arg(decimal, 8, 16, QLatin1Char( '0' ));
                      

                      you can even add a preceding "0x" if you desire, like this:

                      QString hexvalue = QString("0x%1").arg(decimal, 8, 16, QLatin1Char( '0' ));
                      
                      G Offline
                      G Offline
                      georgem
                      wrote on 21 Sept 2015, 05:02 last edited by
                      #10

                      @mp035
                      Thanks for your brilliant answer!

                      1 Reply Last reply
                      0
                      • M mp035
                        20 Nov 2014, 06:29

                        I stumbled across this thread and was very disappointed that no one actually answered the OP's question, unless you count discrediting requirements as an answer. :-)

                        For anyone else looking for an answer, QString::arg is exactly what you are after for this sort of conversion, because it has a "base", "fieldwidth" and a "fillchar" field:

                        QString QString::arg ( int a, int fieldWidth = 0, int base = 10, const QChar & fillChar = QLatin1Char( ' ' ) ) const

                        example:

                        uint decimal = 203888026;
                        QString hexvalue = QString("%1").arg(decimal, 8, 16, QLatin1Char( '0' ));
                        

                        you can even add a preceding "0x" if you desire, like this:

                        QString hexvalue = QString("0x%1").arg(decimal, 8, 16, QLatin1Char( '0' ));
                        
                        N Offline
                        N Offline
                        nirmal
                        wrote on 27 Jun 2020, 11:24 last edited by
                        #11

                        @mp035 Thanks for the answer.

                        1 Reply Last reply
                        0

                        • Login

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