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 0xAABBCCDD to QString DDCCBBAA
Qt 6.11 is out! See what's new in the release blog

Convert 0xAABBCCDD to QString DDCCBBAA

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.6k 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.
  • P Offline
    P Offline
    pVit
    wrote on last edited by
    #1

    Hello,
    I need to convert for example from int 0xAABBCCDD to QString DDCCBBAA. I think it's any function of QByteArray but I can't find more.

    I have this code, but It doesn't work properly, output is last two letters.

    #define NUMBER           0xAAFC
    
        QByteArray array;
        array.append(NUMBER);
    
        qDebug() << QString(array.toHex());
    

    Thanks for help, byte actions aren't my friends.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      The fact that you used symmetric bytes (e.g. AA instead of A1) makes it somewhat ambiguous. See if this suits you

      QString hexString = QString::number(0xA1B2C3D4,16);
      std::reverse(hexString.begin(),hexString.end());
      

      this will produce 4D3C2B1A

      if you wanted D4C3B2A1 instead you can indeed use bitshift:

      QString hexString;
      for(auto number = 0xA1B2C3D4;number;number >>=8)
      hexString.append(QString::number(number & 0xFF,16));
      

      EDIT

      Fixed error in code

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      P 1 Reply Last reply
      3
      • SeenuS Offline
        SeenuS Offline
        Seenu
        wrote on last edited by
        #3

        @pVit

        QString hexString = QString::number(0xAABBCCDD,16);
        std::reverse(hexString.begin(),hexString.end());

        works fine to have upper case
        add
        QString s = hexString.toUpper();
        you will get string in upper case

        1 Reply Last reply
        1
        • VRoninV VRonin

          The fact that you used symmetric bytes (e.g. AA instead of A1) makes it somewhat ambiguous. See if this suits you

          QString hexString = QString::number(0xA1B2C3D4,16);
          std::reverse(hexString.begin(),hexString.end());
          

          this will produce 4D3C2B1A

          if you wanted D4C3B2A1 instead you can indeed use bitshift:

          QString hexString;
          for(auto number = 0xA1B2C3D4;number;number >>=8)
          hexString.append(QString::number(number & 0xFF,16));
          

          EDIT

          Fixed error in code

          P Offline
          P Offline
          pVit
          wrote on last edited by
          #4

          @VRonin
          Thanks a lot. I only fixed number >>=1 to number >>=8 for correct function. Now it works as I need.

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            I only fixed number >>=1 to number >>=8

            Yep, sorry you are correct, I was thinking in bytes for some reason. Need more ☕

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            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