combining bytes to a word
-
Hi
would something likequint16 MakeWord(unsigned char b1 , unsigned char b2) { quint16 base = b1; base <<= 8; base += b2; return base; }
work?
-
@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. -
@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 correspondinghtons()
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 arehtonl()
andntohl()
for long integers as well. -
@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 aQTcpSocket
and read your data. You can do that in several ways and in case you want do deserialize something you can useQDataStream
attached to theQByteArray
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 aQTcpSocket
and read your data. You can do that in several ways and in case you want do deserialize something you can useQDataStream
attached to theQByteArray
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. -
@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 aQDataStream
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.