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

combining bytes to a word

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 5.4k Views 3 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.
  • Z Offline
    Z Offline
    zeroptr
    wrote on last edited by
    #1

    Hi All,

    my device sends bytes it's suitible for mst cases at my application but sometimes I have get WORD (unsigned short int 16 in C++ world) is there any Qt wat to combine two bytes..

    Happy Christmas to All,

    Linux Mint 20.04 64 Bit QT6.0.1

    B 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      would something like

      quint16 MakeWord(unsigned char b1 , unsigned char b2) {
          quint16 base = b1;
          base <<= 8;
          base += b2;
          return base;
      }
      

      work?

      kshegunovK 1 Reply Last reply
      0
      • mrjjM mrjj

        Hi
        would something like

        quint16 MakeWord(unsigned char b1 , unsigned char b2) {
            quint16 base = b1;
            base <<= 8;
            base += b2;
            return base;
        }
        

        work?

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #3

        @mrjj
        I would not use the plus operator for bitwise operations, it just looks wrong (although it should work). This should prove shorter:

        inline quint16 from8Bit(quint8 high, quint8 low)
        {
            return (static_cast<quint16>(high) << 8) | low;
        }
        

        @zeroptr
        As a historical context, WORD is a legacy typedef specific to microsoft from the time when addressing occurred in 16bits (the age of Win 3.1). Actually a "word" is memory addressing term with the size of the (surprise) the memory word. For a modern 64bit system a word is 64bits, and in Qt you have a portable typedef quintptr that is guaranteed to be the size of the pointer on any platform Qt supports, in case you need it.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        0
        • Z Offline
          Z Offline
          zeroptr
          wrote on last edited by
          #4

          I'll try both solution...

          Thanks a lot...

          Linux Mint 20.04 64 Bit QT6.0.1

          mrjjM 1 Reply Last reply
          0
          • Z zeroptr

            I'll try both solution...

            Thanks a lot...

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @zeroptr
            Hi, use the @kshegunov version. much more elegant :)

            1 Reply Last reply
            0
            • Z zeroptr

              Hi All,

              my device sends bytes it's suitible for mst cases at my application but sometimes I have get WORD (unsigned short int 16 in C++ world) is there any Qt wat to combine two bytes..

              Happy Christmas to All,

              B Offline
              B Offline
              bsomervi
              wrote on last edited by bsomervi
              #6

              @zeroptr your device "sends" bytes. How are they sent?

              For networking it is normal to send data larger than single bytes in "network byte order" which is little endian ordered and the function ntohs() is used to recombine a pair of bytes in network byte order into a short integer. This assumes that the sender used the corresponding htons() function to send the short integer as a byte pair in the first place. The significance of the functions is that they encapsulate the endianness of the machine they run on. There are htonl() and ntohl() for long integers as well.

              kshegunovK 1 Reply Last reply
              1
              • Z Offline
                Z Offline
                zeroptr
                wrote on last edited by
                #7

                The stuation is exacly @bsomervi way... This is a controller device sends data via TCP/IP... I'm trying to get values from PC..

                Linux Mint 20.04 64 Bit QT6.0.1

                1 Reply Last reply
                0
                • B bsomervi

                  @zeroptr your device "sends" bytes. How are they sent?

                  For networking it is normal to send data larger than single bytes in "network byte order" which is little endian ordered and the function ntohs() is used to recombine a pair of bytes in network byte order into a short integer. This assumes that the sender used the corresponding htons() function to send the short integer as a byte pair in the first place. The significance of the functions is that they encapsulate the endianness of the machine they run on. There are htonl() and ntohl() for long integers as well.

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by kshegunov
                  #8

                  @bsomervi said:

                  in "network byte order" which is little endian ordered

                  Actually TCP/IP sends them in big endian, so I assume this is a typo.

                  @zeroptr
                  Well, that's a bit different then. Just open a QTcpSocket and read your data. You can do that in several ways and in case you want do deserialize something you can use QDataStream attached to the QByteArray read from the socket.

                  Kind regards.

                  Read and abide by the Qt Code of Conduct

                  B 1 Reply Last reply
                  0
                  • kshegunovK kshegunov

                    @bsomervi said:

                    in "network byte order" which is little endian ordered

                    Actually TCP/IP sends them in big endian, so I assume this is a typo.

                    @zeroptr
                    Well, that's a bit different then. Just open a QTcpSocket and read your data. You can do that in several ways and in case you want do deserialize something you can use QDataStream attached to the QByteArray read from the socket.

                    Kind regards.

                    B Offline
                    B Offline
                    bsomervi
                    wrote on last edited by
                    #9

                    @kshegunov said:

                    @bsomervi said:

                    in "network byte order" which is little endian ordered

                    Actually TCP/IP sends them in big endian, so I assume this is a typo.

                    Yes you are correct. I forget because I always use ntohl and friends so there is no need to remember ;)

                    @zeroptr
                    Well, that's a bit different then. Just open a QTcpSocket and read your data. You can do that in several ways and in case you want do deserialize something you can use QDataStream attached to the QByteArray read from the socket.

                    QDataStream is only really practical where Qt is not being used although sending data ready encoded in a QDataStream format would certainly save a lot of maintenance at the other end.

                    kshegunovK 1 Reply Last reply
                    0
                    • B bsomervi

                      @kshegunov said:

                      @bsomervi said:

                      in "network byte order" which is little endian ordered

                      Actually TCP/IP sends them in big endian, so I assume this is a typo.

                      Yes you are correct. I forget because I always use ntohl and friends so there is no need to remember ;)

                      @zeroptr
                      Well, that's a bit different then. Just open a QTcpSocket and read your data. You can do that in several ways and in case you want do deserialize something you can use QDataStream attached to the QByteArray read from the socket.

                      QDataStream is only really practical where Qt is not being used although sending data ready encoded in a QDataStream format would certainly save a lot of maintenance at the other end.

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by kshegunov
                      #10

                      @bsomervi said:

                      Yes you are correct. I forget because I always use ntohl and friends so there is no need to remember

                      QDataStream is only really practical where Qt is not being used although sending data ready encoded in a QDataStream format would certainly save a lot of maintenance at the other end.

                      I try to use what Qt provides me. There's no point of using a library/framework if you don't actually use it, right? QDataStream provides much more than simple serialization, it can do the byte order transformation in the background and you can have versioning with it, so I really see no point in using nto* whatever system functions if there is a class that does this for me. The point of C++ is to have reusability for your components, and there isn't much benefit in going naked-C-style unless you really need to. just my 2 cents.

                      Read and abide by the Qt Code of Conduct

                      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