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

Int16 to QByteArray

Scheduled Pinned Locked Moved General and Desktop
bitsbytesconversion
11 Posts 6 Posters 6.4k Views 5 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.
  • M Offline
    M Offline
    m.sue
    wrote on 29 Nov 2016, 14:38 last edited by
    #2

    Hi,
    Your code is correct if you run it on a normal PC under, say, Windows 10. I tried it and got: "\x00\x00!\x13" (! is 33 which is 0x21). On which OS do you run the code? Is it maybe a big endian machine?
    -Michael.

    1 Reply Last reply
    1
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 29 Nov 2016, 14:42 last edited by
      #3

      Hi,

      Out of curiosity, why not use QByteArray::number ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • M Offline
        M Offline
        m.sue
        wrote on 29 Nov 2016, 14:49 last edited by m.sue
        #4

        Hi,
        with qDebug() << QByteArray::number(id,16); you will get the four characters "2113". So it depends on what you really need.
        -Michael.

        T 1 Reply Last reply 29 Nov 2016, 15:14
        0
        • T Offline
          T Offline
          TomHoe
          wrote on 29 Nov 2016, 15:06 last edited by
          #5

          @m-sue
          I also got \x00\x00!\x13 on Windows, but didn't knew that '!' is the same as 0x21..

          Is there a way to rewrite it as 0x21 ?

          Thanks!

          J 1 Reply Last reply 30 Nov 2016, 01:21
          0
          • M m.sue
            29 Nov 2016, 14:49

            Hi,
            with qDebug() << QByteArray::number(id,16); you will get the four characters "2113". So it depends on what you really need.
            -Michael.

            T Offline
            T Offline
            TomHoe
            wrote on 29 Nov 2016, 15:14 last edited by
            #6

            @m.sue said in Int16 to QByteArray:

            Hi,
            with qDebug() << QByteArray::number(id,16); you will get the four characters "2113". So it depends on what you really need.
            -Michael.

            @SGaist
            Yes I've tried that function but I'll send the byteArray with TCP to a micro controller after the transformation, so QByteArray::nummer or .setNum is not an option.

            K 1 Reply Last reply 29 Nov 2016, 15:23
            0
            • T TomHoe
              29 Nov 2016, 15:14

              @m.sue said in Int16 to QByteArray:

              Hi,
              with qDebug() << QByteArray::number(id,16); you will get the four characters "2113". So it depends on what you really need.
              -Michael.

              @SGaist
              Yes I've tried that function but I'll send the byteArray with TCP to a micro controller after the transformation, so QByteArray::nummer or .setNum is not an option.

              K Offline
              K Offline
              kshegunov
              Moderators
              wrote on 29 Nov 2016, 15:23 last edited by
              #7

              Use QDataStream over the bytearray.

              qint32 whatever = 5;
              
              QByteArray data;
              QDataStream out(&data, QIODevice::WriteOnly);  // By default is in big endian
              out << whatever;
              

              Read and abide by the Qt Code of Conduct

              T 1 Reply Last reply 29 Nov 2016, 18:13
              2
              • K kshegunov
                29 Nov 2016, 15:23

                Use QDataStream over the bytearray.

                qint32 whatever = 5;
                
                QByteArray data;
                QDataStream out(&data, QIODevice::WriteOnly);  // By default is in big endian
                out << whatever;
                
                T Offline
                T Offline
                TomHoe
                wrote on 29 Nov 2016, 18:13 last edited by
                #8

                @kshegunov Thanks, less code this way. But still x00!\x13 and not 0x00\0x21\0x13.

                Is there a way to rewrite it to regular hex codes ? Or does every controller understand this notation ?

                K 1 Reply Last reply 29 Nov 2016, 18:48
                0
                • M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 29 Nov 2016, 18:36 last edited by
                  #9

                  Hi
                  Will the micro controller also run Qt and be able to use QDataStream ?

                  1 Reply Last reply
                  0
                  • T TomHoe
                    29 Nov 2016, 18:13

                    @kshegunov Thanks, less code this way. But still x00!\x13 and not 0x00\0x21\0x13.

                    Is there a way to rewrite it to regular hex codes ? Or does every controller understand this notation ?

                    K Offline
                    K Offline
                    kshegunov
                    Moderators
                    wrote on 29 Nov 2016, 18:48 last edited by kshegunov
                    #10

                    ! and \0x21 are one and the same binary-wise, I don't understand the question, rewrite it to what? It already is \0x21.

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    1
                    • T TomHoe
                      29 Nov 2016, 15:06

                      @m-sue
                      I also got \x00\x00!\x13 on Windows, but didn't knew that '!' is the same as 0x21..

                      Is there a way to rewrite it as 0x21 ?

                      Thanks!

                      J Offline
                      J Offline
                      JKSH
                      Moderators
                      wrote on 30 Nov 2016, 01:21 last edited by
                      #11

                      @TomHoe said in Int16 to QByteArray:

                      didn't knew that '!' is the same as 0x21..

                      See http://www.ascii-code.com/

                      '!' == 33 == 0x21 == 0b00100001

                      @TomHoe said in Int16 to QByteArray:

                      Is there a way to rewrite it to regular hex codes ? Or does every controller understand this notation ?

                      Again, '!' == 33 == 0x21 == 0b00100001

                      Those are different ways of displaying the same bytes. The controller does not see any "hex" or "notation"; it only sees the sequence of 0's and 1's.

                      I recommend you learn about how bits/bytes are stored in computer memory: http://statmath.wu.ac.at/courses/data-analysis/itdtHTML/node55.html

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      1 Reply Last reply
                      3

                      11/11

                      30 Nov 2016, 01:21

                      • Login

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