Results 1 to 5 of 5

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

  1. #1
    Join Date
    Sep 2012
    Posts
    31
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

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

    Hi, i am currently creating a project that makes use of binary files containing char, ulong and ulonglong datatypes. Opening the file and storing it on a pointer is no problem in Qt for I just use QFile.

    The problem arises when I try to display data of type ulong and ulonglong. Qt reads its from the least significant bit to the most significant bit.

    So for example i will have to read a ulong long datatype:
    (hex) 00 00 00 00 3e 80 00 00 1048576000(in decimal)

    ..when I view the data in memory editor, it appears ok but when I use it to populate my struct, it reads in reverse. My text box displays 80 3e 00 00 00 00.

    Can anyone enlighten me?

    thanks!

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,446
    Thanked 270 Times in 265 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

    Did You know what it is big endian and little endian ?

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,533
    Thanks
    13
    Thanked 1,576 Times in 1,505 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    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.

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

    ehnuh (23rd October 2012)

  5. #4
    Join Date
    Sep 2012
    Posts
    31
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

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

    thank you very much for the replies!

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,356
    Thanks
    3
    Thanked 5,010 Times in 4,789 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

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

    Quote Originally Posted by ChrisW67 View Post
    The functions ntohs() and ntohl() do byte swapping from network to host order for 16- and 32-bit values respectively.
    There are also qFromBigEndian() and qFromLittleEndian() (and their "to" equivalents) template functions in Qt.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    ehnuh (25th 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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.