Results 1 to 5 of 5

Thread: Qt-- Way of reading and writing bytes (4 bytes and 8 bytes)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Qt-- Way of reading and writing bytes (4 bytes and 8 bytes)

    Qt reads bytes from a file in precisely the order they are in the file. If you a reading 4/8 bytes into a patch of memory and not allowing for byte order differences between the file order and your machine's int/long long order then you end up with int/longlong values that don't reflect what you expect.

    The file is in big-endian format (AKA network order) if the bytes are in the order you list. Your processor is most likely little endian (e.g. Intel x86 processors). The first byte in the file (00), the most significant byte from the sender's point of view, ends up in the first byte of the memory set aside for your long long. Your processor treats this as the least-significant byte of the long long.

    The functions ntohs() and ntohl() do byte swapping from network to host order for 16- and 32-bit values respectively. See http://stackoverflow.com/questions/8...bit-ntohl-in-c for 64-bit options.

    With care you could use QDataStream to read the file and handle the swapping for you.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  2. The following user says thank you to ChrisW67 for this useful post:

    ehnuh (23rd October 2012)

Similar Threads

  1. reading bytes out of a file
    By priceey in forum Qt Programming
    Replies: 7
    Last Post: 6th October 2009, 16:55
  2. Reading and writing bytes in file
    By DiamonDogX in forum Qt Programming
    Replies: 2
    Last Post: 20th May 2009, 20:25
  3. Writing raw bytes as text?
    By DiamonDogX in forum Qt Programming
    Replies: 1
    Last Post: 18th May 2009, 16:56
  4. reading 4-bytes integer from binary file
    By maka in forum Qt Programming
    Replies: 8
    Last Post: 12th May 2009, 05:57
  5. How to get an array of bytes from a DLL?
    By srohit24 in forum Qt Programming
    Replies: 5
    Last Post: 22nd April 2009, 16:11

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
  •  
Qt is a trademark of The Qt Company.