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

Int16 to QByteArray

Scheduled Pinned Locked Moved General and Desktop
bitsbytesconversion
11 Posts 6 Posters 6.4k 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.
  • T Offline
    T Offline
    TomHoe
    wrote on last edited by
    #1

    Hello Everyone,

    I am trying to convert a int into 4 bytes, using the following code:

    unsigned int id =  0x2113; // = 8467
    QByteArray name;
    
    name.append((id >> 24) & 0XFF);
    name.append((id >> 16) & 0XFF);
    name.append((id >> 8) & 0XFF);
    name.append(id & 0XFF);
    
    qDebug() << name;
    

    And it works until id is bigger than 0xFF, then the result is like \x00\x00\bA and it has to be \x00\x00\x21\x13

    What is going wrong?

    Thanks

    1 Reply Last reply
    0
    • m.sueM Offline
      m.sueM Offline
      m.sue
      wrote on 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
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on 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.sueM Offline
          m.sueM Offline
          m.sue
          wrote on 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
          0
          • T Offline
            T Offline
            TomHoe
            wrote on 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!

            JKSHJ 1 Reply Last reply
            0
            • m.sueM m.sue

              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 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.

              kshegunovK 1 Reply Last reply
              0
              • T TomHoe

                @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.

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on 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
                2
                • kshegunovK kshegunov

                  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 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 ?

                  kshegunovK 1 Reply Last reply
                  0
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 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

                      @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 ?

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on 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

                        @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!

                        JKSHJ Offline
                        JKSHJ Offline
                        JKSH
                        Moderators
                        wrote on 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

                        • Login

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