I have a QByteArray
which contains the telegram in a special format that is then sent via UDP to a remote machine for further processing. I use QByteArray::append()
to put the telegram together.
One of the components of the telegram is an index: 1, 2, 3, ..., 999. This index needs to be padded with zeroes in the front: 001, 002, 003, ..., 999. In order to do that I initially hard coded the index for testing purposes:
...
telegram.append("001");
...
but later I obviously had to move to a more general solution like
...
QString paddedIdx = QString("%1").arg(idx, 2, QChar('0'));
telegram.append(paddedIdx);
...
The initial try worked without any issues. Looking in the debugged contents of the telegram
I was able to see that "002" is appended to the byte array. However with the new solution I get "0\002" and as a result the processing of the telegram on the remote machine fails (I use reg ex for the parsing). If I'm not mistaken the \0
is the terminating character however it's definitely something I'm not expecting to see in my byte array.