Results 1 to 8 of 8

Thread: PlEASE HELP: Hot To Use Byte Array, data stream

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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: PlEASE HELP: Hot To Use Byte Array, data stream

    The QDataStream writes enough information to reconstruct the item being written. When you write a byte, short, or int you get exactly 1, 2, or 4 bytes. Strings are written with an int indicating the length of the following string, in my example this is 12 bytes (6 x 2bytes per Unicode character). If you want single byte chars you should convert the QString to a char *, but you will still get the leading int indicating size.
    Qt Code:
    1. QDataStream data(&arr,QIODevice::ReadWrite);
    2. data.setVersion(QDataStream::Qt_4_6);
    3. data<< quint16(0xBEEF) <<quint8('S');
    4. data<< message.toAscii();
    5. data<<(quint8('L'));
    6. qDebug() << arr.toHex();
    7. data.device()->seek(0);
    8. data << quint16(arr.size() - sizeof(quint16));
    9. qDebug() << arr.toHex();
    10. data.device()->seek(0);
    11.  
    12. ...
    13.  
    14. "beef53000000064142434445464c"
    15. "000c53000000064142434445464c"
    To copy to clipboard, switch view to plain text mode 

    If you want absolute control of the bytes in the stream you probably don't want QDataStream; look into QBuffer and QByteArray

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

    aash_89 (16th July 2010)

Similar Threads

  1. Replies: 3
    Last Post: 19th April 2010, 15:16
  2. 2D array data plot!
    By kahramonj in forum Qwt
    Replies: 3
    Last Post: 21st March 2009, 12:48
  3. reading utf8 as data stream.
    By kami in forum Newbie
    Replies: 1
    Last Post: 28th September 2008, 22:48
  4. Is it possible to stream data from device with phonon ?
    By Elder Orb in forum Qt-based Software
    Replies: 0
    Last Post: 24th July 2008, 20:58
  5. Creating a Pixmap out of an array of data
    By toratora in forum Qt Programming
    Replies: 2
    Last Post: 5th June 2007, 20:00

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.