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 QString in format of Decimal String to appropiate Hex String format.
Forum Updated to NodeBB v4.3 + New Features

Convert QString in format of Decimal String to appropiate Hex String format.

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 11.1k 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.
  • A Offline
    A Offline
    Ayush Gupta
    wrote on 11 Jul 2019, 08:21 last edited by
    #1

    I have below scenario

    I have QString for bytesize = 1 , byteSize = 2 byteSize =3
    Suppose
    QString test("65") ; [for bytesize = 1 i.e char]
    I need to convert it to hex string and hex string should contain value (41)

    How can i do that in QT?

    G R 2 Replies Last reply 11 Jul 2019, 08:39
    0
    • A Ayush Gupta
      11 Jul 2019, 08:21

      I have below scenario

      I have QString for bytesize = 1 , byteSize = 2 byteSize =3
      Suppose
      QString test("65") ; [for bytesize = 1 i.e char]
      I need to convert it to hex string and hex string should contain value (41)

      How can i do that in QT?

      G Offline
      G Offline
      Gojir4
      wrote on 11 Jul 2019, 08:39 last edited by Gojir4 7 Nov 2019, 11:37
      #2

      @Ayush-Gupta Hi,

      QString test("65") ;
      bool ok = false;
      quint32  iTest = test.toUInt(&ok, 10);
      if(ok) {
          QString hex = QString("%1").arg(iTest, 2, 16);
         //hex contains now "41"
      } else {
         //String to int conversion error
      }
      

      Shorter version (without testing conversion error)

      QString test("65");
      QString hex = QString("%1").arg(test.toUInt(), 16);
      
      1 Reply Last reply
      2
      • A Ayush Gupta
        11 Jul 2019, 08:21

        I have below scenario

        I have QString for bytesize = 1 , byteSize = 2 byteSize =3
        Suppose
        QString test("65") ; [for bytesize = 1 i.e char]
        I need to convert it to hex string and hex string should contain value (41)

        How can i do that in QT?

        R Offline
        R Offline
        raven-worx
        Moderators
        wrote on 11 Jul 2019, 08:59 last edited by raven-worx 7 Nov 2019, 09:21
        #3

        @Ayush-Gupta
        QString("65").toLatin1().toHex();

        --- 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

        G 1 Reply Last reply 11 Jul 2019, 09:11
        0
        • R raven-worx
          11 Jul 2019, 08:59

          @Ayush-Gupta
          QString("65").toLatin1().toHex();

          G Offline
          G Offline
          Gojir4
          wrote on 11 Jul 2019, 09:11 last edited by
          #4

          @raven-worx said in Convert QString in format of Decimal String to appropiate Hex String format.:

          QString("65").toLatin1().toHex();

          This gives "3635" and not "41"

          R 1 Reply Last reply 11 Jul 2019, 09:21
          0
          • G Gojir4
            11 Jul 2019, 09:11

            @raven-worx said in Convert QString in format of Decimal String to appropiate Hex String format.:

            QString("65").toLatin1().toHex();

            This gives "3635" and not "41"

            R Offline
            R Offline
            raven-worx
            Moderators
            wrote on 11 Jul 2019, 09:21 last edited by
            #5

            @Gojir4 said in Convert QString in format of Decimal String to appropiate Hex String format.:

            This gives "3635" and not "41"

            right, my fault

            --- 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
            • A Offline
              A Offline
              Ayush Gupta
              wrote on 11 Jul 2019, 11:20 last edited by
              #6

              No this does not works for me
              consider scenario
              QString test("-23") ; //and its of char size then -23 should be represent

              int32 iTest = test.toUInt(&ok, 10);
              if(ok) {
              QString hex = QString("%1").arg(iTest, 16);
              } else {
              //String to int conversion error
              }

              is converting on base32 or base32 but I want that it should convert on base8

              G 1 Reply Last reply 11 Jul 2019, 11:44
              0
              • A Ayush Gupta
                11 Jul 2019, 11:20

                No this does not works for me
                consider scenario
                QString test("-23") ; //and its of char size then -23 should be represent

                int32 iTest = test.toUInt(&ok, 10);
                if(ok) {
                QString hex = QString("%1").arg(iTest, 16);
                } else {
                //String to int conversion error
                }

                is converting on base32 or base32 but I want that it should convert on base8

                G Offline
                G Offline
                Gojir4
                wrote on 11 Jul 2019, 11:44 last edited by Gojir4 7 Nov 2019, 11:45
                #7

                @Ayush-Gupta Of course that's another story if you want to handle negative numbers.

                QString strIntToHex(const QString &intStr){
                    bool ok = false;
                    qint32  iTest = intStr.toInt(&ok, 10);
                    QString hex;
                    if(ok) {
                        if(iTest >= INT8_MIN && iTest <= INT8_MAX){
                            hex = QString("%1").arg(iTest & 0xFF, 2, 16);
                        } else if(iTest >= INT16_MIN && iTest <= INT16_MAX){
                            hex = QString("%1").arg(iTest & 0xFFFF, 4, 16);
                        } else {
                            hex = QString("%1").arg(iTest, 8, 16);
                        }
                    } else {
                       //String to int conversion error
                        hex = "error";
                    }
                    return hex;
                }
                
                QString test("65");
                qDebug() << strIntToHex("65");
                qDebug() << strIntToHex("-23");
                
                return 0;
                

                Output:

                "41"
                "e9"
                
                1 Reply Last reply
                3

                1/7

                11 Jul 2019, 08:21

                • Login

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