标签/分类 "上位机软件" 下的文章
QT 十六进制字符串和十六进制之间互转(ByteArray和HexString)
举例说明 ByteArray {0x01,0x02,0x03,0x04,0x05,0x06}; HexString "01 02 03 04 05 06" 代码 Convert.cpp #include "convert.h"#include <QString>#include <QMessageBox>#include <QDebug> Convert::Convert() { } QString Convert::ByteArrayToHexString(QByteArray data) { QString ret(data.toHex().toUpper()); int len = ret.length()/2; for(int i=1;i<=len;i++) { ret.insert(2*i+i-1,""); } return ret; } QByteArray Convert::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); }else{ ret.clear(); //ret = NULL; qDebug()<<"非法的16进制字符:"<&l