删除字节数组 : Qt 缓冲区之间的空字符 (\x00)
Remove null character (x00) in between buffer of byte array : Qt
我正在使用readAll()
类QSerialPort
方法从虚拟COM端口读取数据。
我从硬件主板收到两个响应。
1. 字符串("10")
确认
2.查询数据(";1395994881;1.0.0")
字符串数据
作为QByteArray : 10x00;1395994881;1.0.0
我想从QByteArray
中删除x00
角色。
每当我尝试在我的 QByteArray 上执行任何操作时,我都会在QByteArray
的第一部分执行,即 ''10 .跳过;1395994881;1.0.0
的其余部分。
如果有任何方法可以从QByteArray
中删除此x00
字符,请指导我。
代码
QByteArray data = serial.readAll(); //("10x00;1395994881;1.0.0");
-
试用:
QList<QByteArray> list = data.split('x00'); qDebug()<<list; // 10
-
试用:
qDebug()<<data.remove(2, 1); //10
-
试用:
qDebug()<<QString(data); //10
-
试用:
qDebug()<<data.replace('x00', ""); //10
-
试用:
Used Loop to go byte by byte, but same result
无论如何,我希望结果字符串为单个字符串或拆分。但是需要完整的字符串,例如10;1395994881;1.0.0
或10
和;1395994881;1.0.0
.
我还在下面提到Stack Overflow
问题。
1.QByteArray to QString
2.Remove null from string:Java
3.Code Project
编辑
//Function to read data from serial port
void ComPortThread::readData()
{
if(m_isComPortOpen) {
//Receive data from serial port
m_serial->waitForReadyRead(500); //Wait 0.5 sec
QByteArray data = m_serial->readAll();
if(!data.isEmpty()) {
qDebug()<<"Data: "<<data; //10x00;1395994881;1.0.0
//TODO: Separate out two responce like "10" and ";1395994881;1.0.0"
//Problem : Unable to split data into two parts.
emit readyToRead(data);
}
}
}
请帮我解决这个问题。
串行端口返回的QByteArray
是从原始数据创建的,其行为不像字符串。为了处理完整的数组,您可以使用 STL 样式迭代器,它对包含的NULL
字符是健壮的。有关fromRawData
数组的更多信息,请仔细阅读文档中的详细说明。
下面是一个肮脏的示例,显示了如何修复字符串:
const char mydata[] = {
'1', '0', 0x00, ';', '1', '3', '9', '5', '9', '9', '4', '8', '8', '1', ';', '1', '.', '0', '.', '0'
};
QByteArray data = QByteArray::fromRawData(mydata, sizeof(mydata));
qDebug() << data; // "10x00;1395994881;1.0.0"
char fixed[255];
int index = 0;
QByteArray::iterator iter = data.begin();
while(iter != data.end())
{
QChar c = *iter;
if (c != ' ') fixed[index++] = c.toLatin1();
iter++;
}
fixed[index] = ' ';
qDebug() << fixed; // 10;1395994881;1.0.0
- C++字符*缓冲区的大小
- HEX值到wchar_t字符(UTF-8)的转换
- 为什么 Serial.println(<char[]>);返回随机字符?
- 我的字符计数代码计算错误.为什么
- 字符串-C++后显示的随机字符
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 如何在C++中从字符串中分割字符
- 为什么msgrcv()将垃圾字符馈送到缓冲区
- 指向指向字符数组的指针数组的指针
- 如何用转义符替换字符串中的所有特殊字符
- 为什么 sscanf 无法从一个字符串中读取uint64_t和字符?
- 比较字符数组
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 如何在C++中确定文本文件中的元素是字符还是数字
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++中无符号字符溢出
- 将'\x00\x00\x00'格式的字符串转换为无符号字符数组