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 QByteArray to int
QtWS25 Last Chance

Convert QByteArray to int

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 5 Posters 772 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.
  • D Offline
    D Offline
    Damian7546
    wrote on 25 Mar 2023, 19:16 last edited by Damian7546
    #1

    Hi,
    I have QByteArray like below:
    "\x00\x00\x00\x00\x02\x00"
    How convert above array to int value = 200 ?

    In this way : .toInt(nullptr,10) doesn't work.

    A 1 Reply Last reply 25 Mar 2023, 19:58
    0
    • A Axel Spoerl
      25 Mar 2023, 19:58

      @Damian7546
      This code parses the byte array as a string with digits. That’s why it doesn’t work.

      Maybe I overlook something, but I see hex 000000200, which would be an int value of 512, not 200.

      Check out how int 200 was converted into bytes and then implement a reverse conversion.

      D Offline
      D Offline
      Damian7546
      wrote on 26 Mar 2023, 07:03 last edited by Damian7546
      #5

      @Axel-Spoerl said in Convert QByteArray to int:

      Check out how int 200 was converted into bytes and then implement a reverse conversion.

      I do like below:

      QByteArray buf1 = "000000002000"
      QByteArray buf2 = QByteArray::fromHex(buf1);
      

      Consequently in buf2 I have "\x00\x00\x00\x00\x02\x00"

      So,to reverse conversion I do:

      quint16 val = buf2.toHex().toInt(nullptr,10);

      C 1 Reply Last reply 26 Mar 2023, 08:15
      0
      • D Damian7546
        25 Mar 2023, 19:16

        Hi,
        I have QByteArray like below:
        "\x00\x00\x00\x00\x02\x00"
        How convert above array to int value = 200 ?

        In this way : .toInt(nullptr,10) doesn't work.

        A Offline
        A Offline
        Axel Spoerl
        Moderators
        wrote on 25 Mar 2023, 19:58 last edited by Axel Spoerl
        #2

        @Damian7546
        This code parses the byte array as a string with digits. That’s why it doesn’t work.

        Maybe I overlook something, but I see hex 000000200, which would be an int value of 512, not 200.

        Check out how int 200 was converted into bytes and then implement a reverse conversion.

        Software Engineer
        The Qt Company, Oslo

        ? D 2 Replies Last reply 25 Mar 2023, 21:02
        1
        • A Axel Spoerl
          25 Mar 2023, 19:58

          @Damian7546
          This code parses the byte array as a string with digits. That’s why it doesn’t work.

          Maybe I overlook something, but I see hex 000000200, which would be an int value of 512, not 200.

          Check out how int 200 was converted into bytes and then implement a reverse conversion.

          ? Offline
          ? Offline
          A Former User
          wrote on 25 Mar 2023, 21:02 last edited by
          #3
          This post is deleted!
          1 Reply Last reply
          0
          • K Offline
            K Offline
            Kent-Dorfman
            wrote on 26 Mar 2023, 00:42 last edited by Kent-Dorfman
            #4

            basic C language coding: Qt/QByteArray is not required, nor warranted

            uint8_t array[] = { 0x00U, 0x00U, 0x02U, 0x00U };
            uint32_t hostValue = ntohl(*(uint32_t*)array);
            // should return 0x200U, but beware some architectures don't like 
            // dereferencing raw memory as an aritrary type unless guarantees
            // are made that the data is native aligned with the expected data type
            

            A workaround is:

            uint32_t source = 0UL; // source will be properly aligned
            memcpy(&source, array, sizeof(uint32_t));
            uint32_t hostValue = ntohl(source);
            
            1 Reply Last reply
            0
            • A Axel Spoerl
              25 Mar 2023, 19:58

              @Damian7546
              This code parses the byte array as a string with digits. That’s why it doesn’t work.

              Maybe I overlook something, but I see hex 000000200, which would be an int value of 512, not 200.

              Check out how int 200 was converted into bytes and then implement a reverse conversion.

              D Offline
              D Offline
              Damian7546
              wrote on 26 Mar 2023, 07:03 last edited by Damian7546
              #5

              @Axel-Spoerl said in Convert QByteArray to int:

              Check out how int 200 was converted into bytes and then implement a reverse conversion.

              I do like below:

              QByteArray buf1 = "000000002000"
              QByteArray buf2 = QByteArray::fromHex(buf1);
              

              Consequently in buf2 I have "\x00\x00\x00\x00\x02\x00"

              So,to reverse conversion I do:

              quint16 val = buf2.toHex().toInt(nullptr,10);

              C 1 Reply Last reply 26 Mar 2023, 08:15
              0
              • D Damian7546 has marked this topic as solved on 26 Mar 2023, 07:09
              • D Damian7546
                26 Mar 2023, 07:03

                @Axel-Spoerl said in Convert QByteArray to int:

                Check out how int 200 was converted into bytes and then implement a reverse conversion.

                I do like below:

                QByteArray buf1 = "000000002000"
                QByteArray buf2 = QByteArray::fromHex(buf1);
                

                Consequently in buf2 I have "\x00\x00\x00\x00\x02\x00"

                So,to reverse conversion I do:

                quint16 val = buf2.toHex().toInt(nullptr,10);

                C Offline
                C Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 26 Mar 2023, 08:15 last edited by
                #6

                @Damian7546 said in Convert QByteArray to int:

                quint16 val = buf2.toHex().toInt(nullptr,10);

                No, since toInt() converts readable characters written as integers, not binary stuff - please read 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
                0

                6/6

                26 Mar 2023, 08:15

                • Login

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