解决方案/解决方法:
实际上, QByteArray的qDebug()输出被截断为'\\0'字符。 这与QByteArray没有关系; 你甚至不能使用qDebug()输出'\\ 0'字符。 有关解释,请参阅下文。
QByteArray buffer;
buffer.append("hello");
buffer.append('\0');
buffer.append("world");
qDebug() << "GNSS msg (" << buffer.size() << "): " << buffer;
输出:
GNSS msg ( 11 ): "hello
甚至以下任何参数都会被忽略:
qDebug() << "hello" << '\0' << "world";
输出:
hello
您可以通过在调试之前替换字节数组中的特殊字符来解决此“问题”:
QByteArray dbg = buffer; // create a copy to not alter the buffer itself
dbg.replace('\\', "\\\\"); // escape the backslash itself
dbg.replace('\0', "\\0"); // get rid of 0 characters
dbg.replace('"', "\