Linux环境 QT串口通信 16进制收发数据
将QByteArray数据转换为QString 代码片
.
//将QByteArray数据转换为QString
QString ByteArrayToHexxString(QByteArray data)
{
QString strDisplay;
QString str = data.toHex().data();
str = str.toUpper();
for(int i = 0;i < str.length();i+=2)
{
QString st = str.mid(i,2);
strDisplay += st;
strDisplay += " ";
}
return strDisplay;
}
将QString数据转换为QByteArray数据
QByteArray HexStringToByteArray(QString HexString)
{
bool ok;
QByteArray ret;
HexString = HexString.trimmed();
HexString = HexString.simplified();
QStringList sl = HexString.split(" ");
foreach (QString s, sl)
{
if(!s.isEmpty())
{
char c = s.toInt(&ok,16)&0xFF;
if(ok)
{
ret.append(c);
}
}
}
r