combining bytes to a word



  • 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,


  • Qt Champions 2016

    Hi
    would something like

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

    work?


  • Qt Champions 2016

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



  • I'll try both solution...

    Thanks a lot...


  • Qt Champions 2016

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



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



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


  • Qt Champions 2016

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



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


  • Qt Champions 2016

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.