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

    Gojir4G raven-worxR 2 Replies Last reply
    0
    • A Ayush Gupta

      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?

      Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on last edited by Gojir4
      #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

        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?

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by raven-worx
        #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

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

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

          Gojir4G Offline
          Gojir4G Offline
          Gojir4
          wrote on 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"

          raven-worxR 1 Reply Last reply
          0
          • Gojir4G Gojir4

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

            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on 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 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

              Gojir4G 1 Reply Last reply
              0
              • A Ayush Gupta

                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

                Gojir4G Offline
                Gojir4G Offline
                Gojir4
                wrote on last edited by Gojir4
                #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

                • Login

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