PDA

View Full Version : Serializing bytes to QByteArray



Zingam
18th June 2014, 10:30
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:



QString stringByte01 = ui->lineEdit_Byte01->text();
QString stringByte02 = ui->lineEdit_Byte02->text();
QString stringByte03 = ui->lineEdit_Byte03->text();
QString stringByte04 = ui->lineEdit_Byte04->text();

char byte01 = stringByte01.at(0).toLatin1();
quint8 byte02 = (quint8) stringByte02.toUShort();
qint8 byte03 = (qint8) stringByte03.toShort();
quint8 byte04 = (quint8) stringByte04.toShort();

anda_skoa
18th June 2014, 11:48
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,
_

Lesiok
18th June 2014, 11:59
Do not you mean the method QByteArray::fromHex ?

yeye_olive
18th June 2014, 12:04
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).

Zingam
21st June 2014, 14:21
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.

anda_skoa
22nd June 2014, 10:05
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


QByteArray data;
data.resize(4);

data[0] = 0xFA;
data[1] = 0xFB;
data[2] = 0xFC;
data[3] = 0xFD;

socket->write(data);


Cheers,
_