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
Forum Updated to NodeBB v4.3 + New Features

Convert QByteArray to int

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 5 Posters 949 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.
  • D Offline
    D Offline
    Damian7546
    wrote on 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.

    Axel SpoerlA 1 Reply Last reply
    0
    • Axel SpoerlA Axel Spoerl

      @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 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);

      Christian EhrlicherC 1 Reply Last reply
      0
      • D Damian7546

        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.

        Axel SpoerlA Offline
        Axel SpoerlA Offline
        Axel Spoerl
        Moderators
        wrote on 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
        1
        • Axel SpoerlA Axel Spoerl

          @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 last edited by
          #3
          This post is deleted!
          1 Reply Last reply
          0
          • Kent-DorfmanK Offline
            Kent-DorfmanK Offline
            Kent-Dorfman
            wrote on 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);
            

            I light my way forward with the fires of all the bridges I've burned behind me.

            1 Reply Last reply
            0
            • Axel SpoerlA Axel Spoerl

              @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 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);

              Christian EhrlicherC 1 Reply Last reply
              0
              • D Damian7546 has marked this topic as solved on
              • D Damian7546

                @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);

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 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

                • Login

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