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. QString hex translation errors
Qt 6.11 is out! See what's new in the release blog

QString hex translation errors

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 655 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.
  • B Offline
    B Offline
    Bryan Kelly
    wrote on last edited by
    #1

    Windows 11, Qt Creator 12.0.2
    I was doing some hex to binary translations and came across some errors. This is the code I added while verifying how things work. All is fine for 8 and 16 bits, but there are problems for 32 and 64 bits.
    I removed the looping code used for my practics, index variable i is set to zero for the below results.

       union union_int_256
        {
            uint64_t int_64[  4 ];
            uint32_t int_32[  8 ];
            uint16_t int_16[ 16 ];
            uint8_t  int_08[ 32 ];  // 64 hex characters
    };
    …
        QString s08;
        QString s16;
        QString s32;
        QString s64;
    
        bool    b08;
        bool    b16;
        bool    b32;
        bool    b64;
        
        uint8_t  x08, z08;
        uint16_t x16, z16;
        uint32_t x32, z32;
        uint64_t x64, z64;
    
            x08 = binary->int_08[ i ];      // input: 0XE3 , 227
            s08.setNum( x08, 16 );          // s08 =  e3
            z08 = s08.toInt( &b08, 16 );    // z08 =  227,  b08 =  true
    
            x16 = binary->int_16[ i ];      // input: 0XCDE3, 5207
            s16.setNum( x16, 16 );          // s16 =  cde3
            z16 = s16.toInt( &b16, 16 );    // z16 =  5207,  b16 =  true
    
            x32 = binary->int_32[ i ];      // input: 89ABCDE3, 2309737955
            s32.setNum( x32, 16 );          // s32 =  89abcde3
            z32 = s32.toInt( &b32, 16 );    // z32 =  0,  b32 =  false
    
            x64 = binary->int_64[ i ];      // input: E2  0123456789ABCDE3, 81985529216486883
            s64.setNum( x64, 16 );          // s64 =  123456789abcde3
            z64 = s64.toInt( &b64, 16 );    // z64 =  0,  b64 =  false
    
    

    and the variable part of the debugger is captured below
    hex01.png

    Any thoughts?

    Christian EhrlicherC JonBJ 2 Replies Last reply
    0
    • B Bryan Kelly

      Windows 11, Qt Creator 12.0.2
      I was doing some hex to binary translations and came across some errors. This is the code I added while verifying how things work. All is fine for 8 and 16 bits, but there are problems for 32 and 64 bits.
      I removed the looping code used for my practics, index variable i is set to zero for the below results.

         union union_int_256
          {
              uint64_t int_64[  4 ];
              uint32_t int_32[  8 ];
              uint16_t int_16[ 16 ];
              uint8_t  int_08[ 32 ];  // 64 hex characters
      };
      …
          QString s08;
          QString s16;
          QString s32;
          QString s64;
      
          bool    b08;
          bool    b16;
          bool    b32;
          bool    b64;
          
          uint8_t  x08, z08;
          uint16_t x16, z16;
          uint32_t x32, z32;
          uint64_t x64, z64;
      
              x08 = binary->int_08[ i ];      // input: 0XE3 , 227
              s08.setNum( x08, 16 );          // s08 =  e3
              z08 = s08.toInt( &b08, 16 );    // z08 =  227,  b08 =  true
      
              x16 = binary->int_16[ i ];      // input: 0XCDE3, 5207
              s16.setNum( x16, 16 );          // s16 =  cde3
              z16 = s16.toInt( &b16, 16 );    // z16 =  5207,  b16 =  true
      
              x32 = binary->int_32[ i ];      // input: 89ABCDE3, 2309737955
              s32.setNum( x32, 16 );          // s32 =  89abcde3
              z32 = s32.toInt( &b32, 16 );    // z32 =  0,  b32 =  false
      
              x64 = binary->int_64[ i ];      // input: E2  0123456789ABCDE3, 81985529216486883
              s64.setNum( x64, 16 );          // s64 =  123456789abcde3
              z64 = s64.toInt( &b64, 16 );    // z64 =  0,  b64 =  false
      
      

      and the variable part of the debugger is captured below
      hex01.png

      Any thoughts?

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

      @Bryan-Kelly said in QString hex translation errors:

      Any thoughts

      Since you don't tell us what you think is wrong nor provide some compileable code with test data - no.

      I would guess you want to tell us that toInt() can not convert it back? If so - toInt() converts to a int32 according to the documentation.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • B Offline
        B Offline
        Bryan Kelly
        wrote on last edited by
        #3

        Hmmm, I though the results were a bit obvious. The status returned for 8 and 16 bits were true, and false for 32 and 64 bits. The hex to int value for 32 bit translation was zero, incorrect. Both the translations for 64 bit were incorrect.
        I did not recognize anything in the documentation restricting the translate operation of 8 and 16 bits.

        My search results did not return the same page that yours did, and were a bit less descriptiive, but still, did not indicate the 8 and 16 bit limitations.

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          So my I guess is correct. Use the correct toFoo() functions if you want everything greater than an int.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          JonBJ 1 Reply Last reply
          1
          • Christian EhrlicherC Christian Ehrlicher

            So my I guess is correct. Use the correct toFoo() functions if you want everything greater than an int.

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #5

            @Christian-Ehrlicher
            I have not tested OP's code and don't have Qt availability. While what you say may be the case for the uint64_t type, the OP claims it goes wrong for 32-bit (uint32_t ) case as well, which should be int?

            Christian EhrlicherC 1 Reply Last reply
            0
            • JonBJ JonB

              @Christian-Ehrlicher
              I have not tested OP's code and don't have Qt availability. While what you say may be the case for the uint64_t type, the OP claims it goes wrong for 32-bit (uint32_t ) case as well, which should be int?

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @JonB said in QString hex translation errors:

              it goes wrong for 32-bit (uint32_t ) case as well, which should be int?

              u = unsigned

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              1
              • B Bryan Kelly

                Windows 11, Qt Creator 12.0.2
                I was doing some hex to binary translations and came across some errors. This is the code I added while verifying how things work. All is fine for 8 and 16 bits, but there are problems for 32 and 64 bits.
                I removed the looping code used for my practics, index variable i is set to zero for the below results.

                   union union_int_256
                    {
                        uint64_t int_64[  4 ];
                        uint32_t int_32[  8 ];
                        uint16_t int_16[ 16 ];
                        uint8_t  int_08[ 32 ];  // 64 hex characters
                };
                …
                    QString s08;
                    QString s16;
                    QString s32;
                    QString s64;
                
                    bool    b08;
                    bool    b16;
                    bool    b32;
                    bool    b64;
                    
                    uint8_t  x08, z08;
                    uint16_t x16, z16;
                    uint32_t x32, z32;
                    uint64_t x64, z64;
                
                        x08 = binary->int_08[ i ];      // input: 0XE3 , 227
                        s08.setNum( x08, 16 );          // s08 =  e3
                        z08 = s08.toInt( &b08, 16 );    // z08 =  227,  b08 =  true
                
                        x16 = binary->int_16[ i ];      // input: 0XCDE3, 5207
                        s16.setNum( x16, 16 );          // s16 =  cde3
                        z16 = s16.toInt( &b16, 16 );    // z16 =  5207,  b16 =  true
                
                        x32 = binary->int_32[ i ];      // input: 89ABCDE3, 2309737955
                        s32.setNum( x32, 16 );          // s32 =  89abcde3
                        z32 = s32.toInt( &b32, 16 );    // z32 =  0,  b32 =  false
                
                        x64 = binary->int_64[ i ];      // input: E2  0123456789ABCDE3, 81985529216486883
                        s64.setNum( x64, 16 );          // s64 =  123456789abcde3
                        z64 = s64.toInt( &b64, 16 );    // z64 =  0,  b64 =  false
                
                

                and the variable part of the debugger is captured below
                hex01.png

                Any thoughts?

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by JonB
                #7

                @Bryan-Kelly said in QString hex translation errors:

                    x32 = binary->int_32[ i ];      // input: 89ABCDE3, 2309737955
                    s32.setNum( x32, 16 );          // s32 =  89abcde3
                    z32 = s32.toInt( &b32, 16 );    // z32 =  0,  b32 =  false
                

                In the light of what @Christian-Ehrlicher has just said, can you try this with 0-7 instead of 8-F as the leading hex digit? :) Do ir with e.g. s32 = 79abcde3, what's the result? And, try the original but use uint QString::toUInt(bool *ok = nullptr, int base = 10) const. The variants for 64-bit use Long/ULong.

                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