Results 1 to 2 of 2

Thread: reading integer with big/little endian

  1. #1
    Join Date
    Nov 2007
    Posts
    24
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default reading integer with big/little endian

    Hello,

    I'm reading from a tcp server an integer (4-bytes) data
    The tcp server is big endian

    On my client, which is a little endian CPU, I've done this :

    Qt Code:
    1. qint32 v;
    2.  
    3. ...
    4. sck.read(data, 16);
    5.  
    6.  
    7. #if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
    8. v = ntohl(*((qint32*) data));
    9. #endif
    To copy to clipboard, switch view to plain text mode 

    Works pretty well, but sounds "old school". Do I have a better way to do that ?

    Thanks...

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: reading integer with big/little endian

    Minor improvement: instead of those #if you could use Qt's conversion functions. You could even hide all of this in a simple function, e.g. qint32 readInt32(QTcpSocket *).

    If the client and server are both Qt applications, and if you do not care about the details of the protocol they use to communicate, just use a QDataStream on top of the socket.

  3. The following user says thank you to yeye_olive for this useful post:

    stef13013 (5th September 2012)

Similar Threads

  1. reading 4-bytes integer from binary file
    By maka in forum Qt Programming
    Replies: 8
    Last Post: 12th May 2009, 05:57
  2. convert from little endian to big endian
    By adamatic in forum Qt Programming
    Replies: 3
    Last Post: 2nd March 2009, 08:52
  3. Endian
    By coderbob in forum Qt Programming
    Replies: 1
    Last Post: 30th November 2007, 11:49
  4. Converting to integer
    By Salazaar in forum Newbie
    Replies: 2
    Last Post: 15th June 2007, 20:11
  5. Replies: 3
    Last Post: 26th July 2006, 13:41

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.