Results 1 to 6 of 6

Thread: Serializing bytes to QByteArray

  1. #1
    Join Date
    Mar 2011
    Posts
    29
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Serializing bytes to QByteArray

    I need to send/receive 4 bytes over TCP to some device (that I don't have yet). I intend to use QTcpSocket.
    My understanding is that I need to copy/extract these 4 bytes to/from QByteArray and because the device is not using a Qt device I would not be able to use QDataStream. So how should I put 4 bytes into QByteArray, what would be the legit way? I read some posts here on the forum too but I am pretty confused and unsure how to do it.

    These are the bytes. Currently they are from my test/study app:

    Qt Code:
    1. QString stringByte01 = ui->lineEdit_Byte01->text();
    2. QString stringByte02 = ui->lineEdit_Byte02->text();
    3. QString stringByte03 = ui->lineEdit_Byte03->text();
    4. QString stringByte04 = ui->lineEdit_Byte04->text();
    5.  
    6. char byte01 = stringByte01.at(0).toLatin1();
    7. quint8 byte02 = (quint8) stringByte02.toUShort();
    8. qint8 byte03 = (qint8) stringByte03.toShort();
    9. quint8 byte04 = (quint8) stringByte04.toShort();
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Serializing bytes to QByteArray

    I am not sure I understand what problem you are facing?

    Your test code seems to be about converting strings into bytes, not about creating a QByteArray from bytes.

    Is there anything that is not working as expected?

    Cheers,
    _

  3. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Serializing bytes to QByteArray

    Do not you mean the method QByteArray::fromHex ?

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

    Default Re: Serializing bytes to QByteArray

    Your post covers several distinct questions.

    Firstly, it seems you want to build a sequence of four bytes from text that the user provides in QLineEdit widgets. It is unclear how you expect this text to be formatted. Must the text in each QLineEdit consist of exactly one latin character, that you convert to its 1-byte Latin-1 encoding (as in: "a" is converted to 0x61) ?

    Secondly, once you have the four bytes in the form of four char values, you can write them to the QTcpSocket without arranging them in a QByteArray. Just write them one by one using QIODevice:: putChar(), or all in one go by organizing them in a plain C array then by calling QIODevice::write(const char *, qint64). This second option is probably best, since it would also allow you to make the whole treatment (including parsing the text in the QLineEdit) uniform (you could set up an array of pointers to your QLineEdits).

  5. #5
    Join Date
    Mar 2011
    Posts
    29
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Serializing bytes to QByteArray

    Quote Originally Posted by anda_skoa View Post
    I am not sure I understand what problem you are facing?

    Your test code seems to be about converting strings into bytes, not about creating a QByteArray from bytes.

    Is there anything that is not working as expected?

    Cheers,
    _

    I need to send/receive 4 bytes send in a block by a hardware device over TCP. I have no idea how to properly encode/decode the for QTcpSocket.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Serializing bytes to QByteArray

    You don't need to encode/decode anything for sending bytes over a QTcpSocket.
    TCP does not interpret data, it is a byte stream as far as the application layer is concerned.

    Lets say you'd want to send 0xFA, 0xFB, 0xFC and 0xFD
    Qt Code:
    1. data.resize(4);
    2.  
    3. data[0] = 0xFA;
    4. data[1] = 0xFB;
    5. data[2] = 0xFC;
    6. data[3] = 0xFD;
    7.  
    8. socket->write(data);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

Similar Threads

  1. Replies: 4
    Last Post: 23rd October 2012, 08:40
  2. Replies: 2
    Last Post: 22nd September 2012, 13:22
  3. Serializing QHash to a QByteArray
    By PeterThePuter in forum Qt Programming
    Replies: 3
    Last Post: 17th December 2010, 23:19
  4. Serializing QImage
    By lukass in forum Newbie
    Replies: 1
    Last Post: 26th October 2010, 18:58
  5. Serializing
    By xyzt in forum Qt Programming
    Replies: 1
    Last Post: 23rd March 2008, 08:51

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.