Results 1 to 8 of 8

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

  1. #1
    Join Date
    Jul 2010
    Posts
    24
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

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

    Hey all.
    this is part of my code

    QDataStream data(&arr,QIODevice::ReadWrite);
    data.setVersion(QDataStream::Qt_4_6);
    data<<(quint16(0))<<quint8('S');
    data<<message;
    data<<(quint8('L'));
    data.device()->seek(0);
    data<<(arr.size()-sizeof(quint16));
    data.device()->seek(0);
    QString a;
    quint8 type1,type2;
    quint16 size;
    data>>size>>type1>>a>>type2;
    ui->textBrowser->append(QString::number(size));
    ui->textBrowser->append(QString::number(type1));
    ui->textBrowser->append(a);
    ui->textBrowser->append(QString::number(type2));


    message is a non empty QString;
    arr is a ByteArray;

    On the text browser i get the output as
    0
    0

    0
    Exactly as above.
    what is happening to all the data im writing! why is it all showing 0??

    Please help


    Im actually writing this data block over a stream using TCPsocket, but i wasnt reciving any data at the other end, so i just did the above to see if data is being written at the source!!

  2. #2
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: PlEASE HELP: Hot To Use Byte Array, data stream

    Look at the detail description of QBuffer class.

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

    Default Re: PlEASE HELP: Hot To Use Byte Array, data stream

    Qt Code:
    1. QDataStream data(&arr,QIODevice::ReadWrite);
    2. data.setVersion(QDataStream::Qt_4_6);
    3. data<<(quint16(0))<<quint8('S');
    4. data<<message;
    5. data<<(quint8('L'));
    6. data.device()->seek(0);
    7. data<<(arr.size()-sizeof(quint16)); // How many bytes are being written here?
    8. tBrowser->append(QString::number(type2));
    To copy to clipboard, switch view to plain text mode 

    When I try a quick test:
    Qt Code:
    1. QString message("ABCDEF");
    2.  
    3. QDataStream data(&arr,QIODevice::ReadWrite);
    4. data.setVersion(QDataStream::Qt_4_6);
    5. data<< quint16(0xBEEF) <<quint8('S');
    6. data<<message;
    7. data<<(quint8('L'));
    8. qDebug() << arr.toHex();
    9. data.device()->seek(0);
    10. data << (arr.size() - sizeof(quint16));
    11. qDebug() << arr.toHex();
    12. data.device()->seek(0);
    To copy to clipboard, switch view to plain text mode 
    I get 18 bytes (0x00000012 bytes) excluding the size field:
    Qt Code:
    1. "beef530000000c0041004200430044004500464c"
    2. "0000001200000c0041004200430044004500464c"
    To copy to clipboard, switch view to plain text mode 
    I think you can see the problem now.

    PS: Were you expecting Unicode characters at the receiving end?
    Last edited by ChrisW67; 16th July 2010 at 06:46. Reason: Added afterthought

  4. #4
    Join Date
    Jul 2010
    Posts
    24
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: PlEASE HELP: Hot To Use Byte Array, data stream

    Thank you Chris for your reply!!

    Im really new to Qt development, 1 week into it.

    I wrote a client server application, the the code snippet i pasted was from the client side. i was trying to send data to the server using QTcpSocket.

    The socket was conncted sucessfully, problem was with the data! so i wanted to see if data is being written to the Bytearray or not by writing data to it and reading from it and displaying it on a text browser.

    the byte array is a block of data i want to send.

    the first one is the size of the data block, since i dont know the size of the wole block i initially write it to 0, the next is a character to 'S' signifying start of the data block. the next block is the data i want to be sending, its a QString now, but later want to be able to send images. the last block is the charater 'L' signifying end of the block.

    after wrtiting the end, i calculate the size of the QByteArray, then use seek(0) to go to the first block, and rewrite the size.


    and finally send this packet of information.

    but when i read out of it, i just get null values.

    I dont understand why.

    Please forgive my inexperience.

    thank you
    aashish

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

    Default Re: PlEASE HELP: Hot To Use Byte Array, data stream

    Take a close look at the first four bytes (8 characters) of the hex dumps: the first is before the size is updated, the second after. I put 0xBEEF into the size as a default value to make it more obvious. You allocate two bytes to hold the size, but write four bytes when you store the size in the data stream. Look at what happens when I write the size the way you did.

    0x53 is 'S'
    0x0041, 0x0042 ... 0x0046 are the letters ABCDEF as unicode
    0x4c is 'L'.

  6. #6
    Join Date
    Jul 2010
    Posts
    24
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: PlEASE HELP: Hot To Use Byte Array, data stream

    1."beef530000000c0041004200430044004500464c"
    2."0000001200000c0041004200430044004500464c"

    Thank you, i understand now.

    If the size is 18, 2 bytes is enough to represent it right?
    why is it taking 4 bytes? how can i make it take 2 bytes only.

    what i mean to ask is, the size is being represented as 00000012, why not just 12.

    also after 's' there is "0000000c" 4 bytes of you did not write but its there!


    i was under the impression that, the bytes should look like this

    120c4142434445464c

    as each char of "ABCDEF" should take 1 byte.

    am I not understanding something correctly here?

    thank you for your guidance.

  7. #7
    Join Date
    Jul 2010
    Posts
    24
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: PlEASE HELP: Hot To Use Byte Array, data stream

    many thnakx chris for your help.
    i just size constrained the last line and it works.

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

  9. 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, 14:16
  2. 2D array data plot!
    By kahramonj in forum Qwt
    Replies: 3
    Last Post: 21st March 2009, 11:48
  3. reading utf8 as data stream.
    By kami in forum Newbie
    Replies: 1
    Last Post: 28th September 2008, 21: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, 19:58
  5. Creating a Pixmap out of an array of data
    By toratora in forum Qt Programming
    Replies: 2
    Last Post: 5th June 2007, 19: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.