diff --git a/comtool/api/quiwidget.cpp b/comtool/api/quiwidget.cpp index a4524c3..1fe4ced 100644 --- a/comtool/api/quiwidget.cpp +++ b/comtool/api/quiwidget.cpp @@ -2968,103 +2968,117 @@ QByteArray QUIHelper::getCRCCode(const QByteArray &data) return QUIHelper::ushortToByteRec(result); } +static QMap listChar; +void QUIHelper::initAsciiStr() +{ + //0x20为空格,空格以下都是不可见字符 + if (listChar.count() == 0) { + listChar.insert(0, "\\NUL"); + listChar.insert(1, "\\SOH"); + listChar.insert(2, "\\STX"); + listChar.insert(3, "\\ETX"); + listChar.insert(4, "\\EOT"); + listChar.insert(5, "\\ENQ"); + listChar.insert(6, "\\ACK"); + listChar.insert(7, "\\BEL"); + listChar.insert(8, "\\BS"); + listChar.insert(9, "\\HT"); + listChar.insert(10, "\\LF"); + listChar.insert(11, "\\VT"); + listChar.insert(12, "\\FF"); + listChar.insert(13, "\\CR"); + listChar.insert(14, "\\SO"); + listChar.insert(15, "\\SI"); + listChar.insert(16, "\\DLE"); + listChar.insert(17, "\\DC1"); + listChar.insert(18, "\\DC2"); + listChar.insert(19, "\\DC3"); + listChar.insert(20, "\\DC4"); + listChar.insert(21, "\\NAK"); + listChar.insert(22, "\\SYN"); + listChar.insert(23, "\\ETB"); + listChar.insert(24, "\\CAN"); + listChar.insert(25, "\\EM"); + listChar.insert(26, "\\SUB"); + listChar.insert(27, "\\ESC"); + listChar.insert(28, "\\FS"); + listChar.insert(29, "\\GS"); + listChar.insert(30, "\\RS"); + listChar.insert(31, "\\US"); + listChar.insert(0x5C, "\\"); + listChar.insert(0x7F, "\\DEL"); + } +} + QString QUIHelper::byteArrayToAsciiStr(const QByteArray &data) { + initAsciiStr(); QString temp; int len = data.size(); - for (int i = 0; i < len; i++) { - //0x20为空格,空格以下都是不可见字符 - char b = data.at(i); - if (0x00 == b) { - temp += QString("\\NUL"); - } else if (0x01 == b) { - temp += QString("\\SOH"); - } else if (0x02 == b) { - temp += QString("\\STX"); - } else if (0x03 == b) { - temp += QString("\\ETX"); - } else if (0x04 == b) { - temp += QString("\\EOT"); - } else if (0x05 == b) { - temp += QString("\\ENQ"); - } else if (0x06 == b) { - temp += QString("\\ACK"); - } else if (0x07 == b) { - temp += QString("\\BEL"); - } else if (0x08 == b) { - temp += QString("\\BS"); - } else if (0x09 == b) { - temp += QString("\\HT"); - } else if (0x0A == b) { - temp += QString("\\LF"); - } else if (0x0B == b) { - temp += QString("\\VT"); - } else if (0x0C == b) { - temp += QString("\\FF"); - } else if (0x0D == b) { - temp += QString("\\CR"); - } else if (0x0E == b) { - temp += QString("\\SO"); - } else if (0x0F == b) { - temp += QString("\\SI"); - } else if (0x10 == b) { - temp += QString("\\DLE"); - } else if (0x11 == b) { - temp += QString("\\DC1"); - } else if (0x12 == b) { - temp += QString("\\DC2"); - } else if (0x13 == b) { - temp += QString("\\DC3"); - } else if (0x14 == b) { - temp += QString("\\DC4"); - } else if (0x15 == b) { - temp += QString("\\NAK"); - } else if (0x16 == b) { - temp += QString("\\SYN"); - } else if (0x17 == b) { - temp += QString("\\ETB"); - } else if (0x18 == b) { - temp += QString("\\CAN"); - } else if (0x19 == b) { - temp += QString("\\EM"); - } else if (0x1A == b) { - temp += QString("\\SUB"); - } else if (0x1B == b) { - temp += QString("\\ESC"); - } else if (0x1C == b) { - temp += QString("\\FS"); - } else if (0x1D == b) { - temp += QString("\\GS"); - } else if (0x1E == b) { - temp += QString("\\RS"); - } else if (0x1F == b) { - temp += QString("\\US"); - } else if (0x7F == b) { - temp += QString("\\x7F"); - } else if (0x5C == b) { - temp += QString("\\x5C"); - } else if (0x20 >= b) { - temp += QString("\\x%1").arg(decimalToStrHex((quint8)b)); + for (int i = 0; i < len; i++) { + char byte = data.at(i); + QString value = listChar.value(byte); + if (!value.isEmpty()) { + } else if (byte >= 0 && byte <= 0x7F) { + value = QString("%1").arg(byte); } else { - temp += QString("%1").arg(b); + value = decimalToStrHex((quint8)byte); + value = QString("\\x%1").arg(value.toUpper()); } + + temp += value; } return temp.trimmed(); } -QByteArray QUIHelper::hexStrToByteArray(const QString &str) +QByteArray QUIHelper::asciiStrToByteArray(const QString &data) +{ + initAsciiStr(); + QByteArray buffer; + QStringList list = data.split("\\"); + + int count = list.count(); + for (int i = 1; i < count; i++) { + QString str = list.at(i); + int key = 0; + if (str.contains("x")) { + key = strHexToDecimal(str.mid(1, 2)); + } else { + key = listChar.key("\\" + str); + } + + buffer.append(key); + } + + return buffer; +} + +char QUIHelper::hexStrToChar(char data) +{ + if ((data >= '0') && (data <= '9')) { + return data - 0x30; + } else if ((data >= 'A') && (data <= 'F')) { + return data - 'A' + 10; + } else if ((data >= 'a') && (data <= 'f')) { + return data - 'a' + 10; + } else { + return (-1); + } +} + +QByteArray QUIHelper::hexStrToByteArray(const QString &data) { QByteArray senddata; int hexdata, lowhexdata; int hexdatalen = 0; - int len = str.length(); + int len = data.length(); senddata.resize(len / 2); char lstr, hstr; for (int i = 0; i < len;) { - hstr = str.at(i).toLatin1(); + hstr = data.at(i).toLatin1(); if (hstr == ' ') { i++; continue; @@ -3075,9 +3089,9 @@ QByteArray QUIHelper::hexStrToByteArray(const QString &str) break; } - lstr = str.at(i).toLatin1(); - hexdata = convertHexChar(hstr); - lowhexdata = convertHexChar(lstr); + lstr = data.at(i).toLatin1(); + hexdata = hexStrToChar(hstr); + lowhexdata = hexStrToChar(lstr); if ((hexdata == 16) || (lowhexdata == 16)) { break; @@ -3094,300 +3108,6 @@ QByteArray QUIHelper::hexStrToByteArray(const QString &str) return senddata; } -char QUIHelper::convertHexChar(char ch) -{ - if ((ch >= '0') && (ch <= '9')) { - return ch - 0x30; - } else if ((ch >= 'A') && (ch <= 'F')) { - return ch - 'A' + 10; - } else if ((ch >= 'a') && (ch <= 'f')) { - return ch - 'a' + 10; - } else { - return (-1); - } -} - -QByteArray QUIHelper::asciiStrToByteArray(const QString &str) -{ - QByteArray buffer; - int len = str.length(); - QString letter; - QString hex; - - for (int i = 0; i < len; i++) { - letter = str.at(i); - - if (letter == "\\") { - i++; - letter = str.mid(i, 1); - - if (letter == "x") { - i++; - hex = str.mid(i, 2); - buffer.append(strHexToDecimal(hex)); - i++; - continue; - } else if (letter == "N") { - i++; - hex = str.mid(i, 1); - - if (hex == "U") { - i++; - hex = str.mid(i, 1); - - if (hex == "L") { //NUL=0x00 - buffer.append((char)0x00); - continue; - } - } else if (hex == "A") { - i++; - hex = str.mid(i, 1); - - if (hex == "K") { //NAK=0x15 - buffer.append(0x15); - continue; - } - } - } else if (letter == "S") { - i++; - hex = str.mid(i, 1); - - if (hex == "O") { - i++; - hex = str.mid(i, 1); - - if (hex == "H") { //SOH=0x01 - buffer.append(0x01); - continue; - } else { //SO=0x0E - buffer.append(0x0E); - i--; - continue; - } - } else if (hex == "T") { - i++; - hex = str.mid(i, 1); - - if (hex == "X") { //STX=0x02 - buffer.append(0x02); - continue; - } - } else if (hex == "I") { //SI=0x0F - buffer.append(0x0F); - continue; - } else if (hex == "Y") { - i++; - hex = str.mid(i, 1); - - if (hex == "N") { //SYN=0x16 - buffer.append(0x16); - continue; - } - } else if (hex == "U") { - i++; - hex = str.mid(i, 1); - - if (hex == "B") { //SUB=0x1A - buffer.append(0x1A); - continue; - } - } - } else if (letter == "E") { - i++; - hex = str.mid(i, 1); - - if (hex == "T") { - i++; - hex = str.mid(i, 1); - - if (hex == "X") { //ETX=0x03 - buffer.append(0x03); - continue; - } else if (hex == "B") { //ETB=0x17 - buffer.append(0x17); - continue; - } - } else if (hex == "O") { - i++; - hex = str.mid(i, 1); - - if (hex == "T") { //EOT=0x04 - buffer.append(0x04); - continue; - } - } else if (hex == "N") { - i++; - hex = str.mid(i, 1); - - if (hex == "Q") { //ENQ=0x05 - buffer.append(0x05); - continue; - } - } else if (hex == "M") { //EM=0x19 - buffer.append(0x19); - continue; - } else if (hex == "S") { - i++; - hex = str.mid(i, 1); - - if (hex == "C") { //ESC=0x1B - buffer.append(0x1B); - continue; - } - } - } else if (letter == "A") { - i++; - hex = str.mid(i, 1); - - if (hex == "C") { - i++; - hex = str.mid(i, 1); - - if (hex == "K") { //ACK=0x06 - buffer.append(0x06); - continue; - } - } - } else if (letter == "B") { - i++; - hex = str.mid(i, 1); - - if (hex == "E") { - i++; - hex = str.mid(i, 1); - - if (hex == "L") { //BEL=0x07 - buffer.append(0x07); - continue; - } - } else if (hex == "S") { //BS=0x08 - buffer.append(0x08); - continue; - } - } else if (letter == "C") { - i++; - hex = str.mid(i, 1); - - if (hex == "R") { //CR=0x0D - buffer.append(0x0D); - continue; - } else if (hex == "A") { - i++; - hex = str.mid(i, 1); - - if (hex == "N") { //CAN=0x18 - buffer.append(0x18); - continue; - } - } - } else if (letter == "D") { - i++; - hex = str.mid(i, 1); - - if (hex == "L") { - i++; - hex = str.mid(i, 1); - - if (hex == "E") { //DLE=0x10 - buffer.append(0x10); - continue; - } - } else if (hex == "C") { - i++; - hex = str.mid(i, 1); - - if (hex == "1") { //DC1=0x11 - buffer.append(0x11); - continue; - } else if (hex == "2") { //DC2=0x12 - buffer.append(0x12); - continue; - } else if (hex == "3") { //DC3=0x13 - buffer.append(0x13); - continue; - } else if (hex == "4") { //DC2=0x14 - buffer.append(0x14); - continue; - } - } - } else if (letter == "F") { - i++; - hex = str.mid(i, 1); - - if (hex == "F") { //FF=0x0C - buffer.append(0x0C); - continue; - } else if (hex == "S") { //FS=0x1C - buffer.append(0x1C); - continue; - } - } else if (letter == "H") { - i++; - hex = str.mid(i, 1); - - if (hex == "T") { //HT=0x09 - buffer.append(0x09); - continue; - } - } else if (letter == "L") { - i++; - hex = str.mid(i, 1); - - if (hex == "F") { //LF=0x0A - buffer.append(0x0A); - continue; - } - } else if (letter == "G") { - i++; - hex = str.mid(i, 1); - - if (hex == "S") { //GS=0x1D - buffer.append(0x1D); - continue; - } - } else if (letter == "R") { - i++; - hex = str.mid(i, 1); - - if (hex == "S") { //RS=0x1E - buffer.append(0x1E); - continue; - } - } else if (letter == "U") { - i++; - hex = str.mid(i, 1); - - if (hex == "S") { //US=0x1F - buffer.append(0x1F); - continue; - } - } else if (letter == "V") { - i++; - hex = str.mid(i, 1); - - if (hex == "T") { //VT=0x0B - buffer.append(0x0B); - continue; - } - } else if (letter == "\\") { - //如果连着的是多个\\则对应添加\对应的16进制0x5C - buffer.append(0x5C); - continue; - } else { - //将对应的\[前面的\\也要加入 - buffer.append(0x5C); - buffer.append(letter.toLatin1()); - continue; - } - } - - buffer.append(str.mid(i, 1).toLatin1()); - - } - - return buffer; -} - QString QUIHelper::byteArrayToHexStr(const QByteArray &data) { QString temp = ""; diff --git a/comtool/api/quiwidget.h b/comtool/api/quiwidget.h index 589f8d6..3b93958 100644 --- a/comtool/api/quiwidget.h +++ b/comtool/api/quiwidget.h @@ -716,15 +716,14 @@ public: static quint16 getModbus16(quint8 *data, int len); static QByteArray getCRCCode(const QByteArray &data); - //字节数组转Ascii字符串 + //字节数组与Ascii字符串互转 + static void initAsciiStr(); static QString byteArrayToAsciiStr(const QByteArray &data); - //16进制字符串转字节数组 - static QByteArray hexStrToByteArray(const QString &str); - static char convertHexChar(char ch); + static QByteArray asciiStrToByteArray(const QString &data); - //Ascii字符串转字节数组 - static QByteArray asciiStrToByteArray(const QString &str); - //字节数组转16进制字符串 + //16进制字符串与字节数组互转 + static char hexStrToChar(char data); + static QByteArray hexStrToByteArray(const QString &data); static QString byteArrayToHexStr(const QByteArray &data); //获取保存的文件 diff --git a/comtool/form/frmcomtool.cpp b/comtool/form/frmcomtool.cpp index 2cd21af..35cbdb1 100644 --- a/comtool/form/frmcomtool.cpp +++ b/comtool/form/frmcomtool.cpp @@ -366,7 +366,6 @@ void frmComTool::sendData(QString data) } QByteArray buffer; - if (ui->ckHexSend->isChecked()) { buffer = QUIHelper::hexStrToByteArray(data); } else { diff --git a/nettool/api/quiwidget.cpp b/nettool/api/quiwidget.cpp index a4524c3..1fe4ced 100644 --- a/nettool/api/quiwidget.cpp +++ b/nettool/api/quiwidget.cpp @@ -2968,103 +2968,117 @@ QByteArray QUIHelper::getCRCCode(const QByteArray &data) return QUIHelper::ushortToByteRec(result); } +static QMap listChar; +void QUIHelper::initAsciiStr() +{ + //0x20为空格,空格以下都是不可见字符 + if (listChar.count() == 0) { + listChar.insert(0, "\\NUL"); + listChar.insert(1, "\\SOH"); + listChar.insert(2, "\\STX"); + listChar.insert(3, "\\ETX"); + listChar.insert(4, "\\EOT"); + listChar.insert(5, "\\ENQ"); + listChar.insert(6, "\\ACK"); + listChar.insert(7, "\\BEL"); + listChar.insert(8, "\\BS"); + listChar.insert(9, "\\HT"); + listChar.insert(10, "\\LF"); + listChar.insert(11, "\\VT"); + listChar.insert(12, "\\FF"); + listChar.insert(13, "\\CR"); + listChar.insert(14, "\\SO"); + listChar.insert(15, "\\SI"); + listChar.insert(16, "\\DLE"); + listChar.insert(17, "\\DC1"); + listChar.insert(18, "\\DC2"); + listChar.insert(19, "\\DC3"); + listChar.insert(20, "\\DC4"); + listChar.insert(21, "\\NAK"); + listChar.insert(22, "\\SYN"); + listChar.insert(23, "\\ETB"); + listChar.insert(24, "\\CAN"); + listChar.insert(25, "\\EM"); + listChar.insert(26, "\\SUB"); + listChar.insert(27, "\\ESC"); + listChar.insert(28, "\\FS"); + listChar.insert(29, "\\GS"); + listChar.insert(30, "\\RS"); + listChar.insert(31, "\\US"); + listChar.insert(0x5C, "\\"); + listChar.insert(0x7F, "\\DEL"); + } +} + QString QUIHelper::byteArrayToAsciiStr(const QByteArray &data) { + initAsciiStr(); QString temp; int len = data.size(); - for (int i = 0; i < len; i++) { - //0x20为空格,空格以下都是不可见字符 - char b = data.at(i); - if (0x00 == b) { - temp += QString("\\NUL"); - } else if (0x01 == b) { - temp += QString("\\SOH"); - } else if (0x02 == b) { - temp += QString("\\STX"); - } else if (0x03 == b) { - temp += QString("\\ETX"); - } else if (0x04 == b) { - temp += QString("\\EOT"); - } else if (0x05 == b) { - temp += QString("\\ENQ"); - } else if (0x06 == b) { - temp += QString("\\ACK"); - } else if (0x07 == b) { - temp += QString("\\BEL"); - } else if (0x08 == b) { - temp += QString("\\BS"); - } else if (0x09 == b) { - temp += QString("\\HT"); - } else if (0x0A == b) { - temp += QString("\\LF"); - } else if (0x0B == b) { - temp += QString("\\VT"); - } else if (0x0C == b) { - temp += QString("\\FF"); - } else if (0x0D == b) { - temp += QString("\\CR"); - } else if (0x0E == b) { - temp += QString("\\SO"); - } else if (0x0F == b) { - temp += QString("\\SI"); - } else if (0x10 == b) { - temp += QString("\\DLE"); - } else if (0x11 == b) { - temp += QString("\\DC1"); - } else if (0x12 == b) { - temp += QString("\\DC2"); - } else if (0x13 == b) { - temp += QString("\\DC3"); - } else if (0x14 == b) { - temp += QString("\\DC4"); - } else if (0x15 == b) { - temp += QString("\\NAK"); - } else if (0x16 == b) { - temp += QString("\\SYN"); - } else if (0x17 == b) { - temp += QString("\\ETB"); - } else if (0x18 == b) { - temp += QString("\\CAN"); - } else if (0x19 == b) { - temp += QString("\\EM"); - } else if (0x1A == b) { - temp += QString("\\SUB"); - } else if (0x1B == b) { - temp += QString("\\ESC"); - } else if (0x1C == b) { - temp += QString("\\FS"); - } else if (0x1D == b) { - temp += QString("\\GS"); - } else if (0x1E == b) { - temp += QString("\\RS"); - } else if (0x1F == b) { - temp += QString("\\US"); - } else if (0x7F == b) { - temp += QString("\\x7F"); - } else if (0x5C == b) { - temp += QString("\\x5C"); - } else if (0x20 >= b) { - temp += QString("\\x%1").arg(decimalToStrHex((quint8)b)); + for (int i = 0; i < len; i++) { + char byte = data.at(i); + QString value = listChar.value(byte); + if (!value.isEmpty()) { + } else if (byte >= 0 && byte <= 0x7F) { + value = QString("%1").arg(byte); } else { - temp += QString("%1").arg(b); + value = decimalToStrHex((quint8)byte); + value = QString("\\x%1").arg(value.toUpper()); } + + temp += value; } return temp.trimmed(); } -QByteArray QUIHelper::hexStrToByteArray(const QString &str) +QByteArray QUIHelper::asciiStrToByteArray(const QString &data) +{ + initAsciiStr(); + QByteArray buffer; + QStringList list = data.split("\\"); + + int count = list.count(); + for (int i = 1; i < count; i++) { + QString str = list.at(i); + int key = 0; + if (str.contains("x")) { + key = strHexToDecimal(str.mid(1, 2)); + } else { + key = listChar.key("\\" + str); + } + + buffer.append(key); + } + + return buffer; +} + +char QUIHelper::hexStrToChar(char data) +{ + if ((data >= '0') && (data <= '9')) { + return data - 0x30; + } else if ((data >= 'A') && (data <= 'F')) { + return data - 'A' + 10; + } else if ((data >= 'a') && (data <= 'f')) { + return data - 'a' + 10; + } else { + return (-1); + } +} + +QByteArray QUIHelper::hexStrToByteArray(const QString &data) { QByteArray senddata; int hexdata, lowhexdata; int hexdatalen = 0; - int len = str.length(); + int len = data.length(); senddata.resize(len / 2); char lstr, hstr; for (int i = 0; i < len;) { - hstr = str.at(i).toLatin1(); + hstr = data.at(i).toLatin1(); if (hstr == ' ') { i++; continue; @@ -3075,9 +3089,9 @@ QByteArray QUIHelper::hexStrToByteArray(const QString &str) break; } - lstr = str.at(i).toLatin1(); - hexdata = convertHexChar(hstr); - lowhexdata = convertHexChar(lstr); + lstr = data.at(i).toLatin1(); + hexdata = hexStrToChar(hstr); + lowhexdata = hexStrToChar(lstr); if ((hexdata == 16) || (lowhexdata == 16)) { break; @@ -3094,300 +3108,6 @@ QByteArray QUIHelper::hexStrToByteArray(const QString &str) return senddata; } -char QUIHelper::convertHexChar(char ch) -{ - if ((ch >= '0') && (ch <= '9')) { - return ch - 0x30; - } else if ((ch >= 'A') && (ch <= 'F')) { - return ch - 'A' + 10; - } else if ((ch >= 'a') && (ch <= 'f')) { - return ch - 'a' + 10; - } else { - return (-1); - } -} - -QByteArray QUIHelper::asciiStrToByteArray(const QString &str) -{ - QByteArray buffer; - int len = str.length(); - QString letter; - QString hex; - - for (int i = 0; i < len; i++) { - letter = str.at(i); - - if (letter == "\\") { - i++; - letter = str.mid(i, 1); - - if (letter == "x") { - i++; - hex = str.mid(i, 2); - buffer.append(strHexToDecimal(hex)); - i++; - continue; - } else if (letter == "N") { - i++; - hex = str.mid(i, 1); - - if (hex == "U") { - i++; - hex = str.mid(i, 1); - - if (hex == "L") { //NUL=0x00 - buffer.append((char)0x00); - continue; - } - } else if (hex == "A") { - i++; - hex = str.mid(i, 1); - - if (hex == "K") { //NAK=0x15 - buffer.append(0x15); - continue; - } - } - } else if (letter == "S") { - i++; - hex = str.mid(i, 1); - - if (hex == "O") { - i++; - hex = str.mid(i, 1); - - if (hex == "H") { //SOH=0x01 - buffer.append(0x01); - continue; - } else { //SO=0x0E - buffer.append(0x0E); - i--; - continue; - } - } else if (hex == "T") { - i++; - hex = str.mid(i, 1); - - if (hex == "X") { //STX=0x02 - buffer.append(0x02); - continue; - } - } else if (hex == "I") { //SI=0x0F - buffer.append(0x0F); - continue; - } else if (hex == "Y") { - i++; - hex = str.mid(i, 1); - - if (hex == "N") { //SYN=0x16 - buffer.append(0x16); - continue; - } - } else if (hex == "U") { - i++; - hex = str.mid(i, 1); - - if (hex == "B") { //SUB=0x1A - buffer.append(0x1A); - continue; - } - } - } else if (letter == "E") { - i++; - hex = str.mid(i, 1); - - if (hex == "T") { - i++; - hex = str.mid(i, 1); - - if (hex == "X") { //ETX=0x03 - buffer.append(0x03); - continue; - } else if (hex == "B") { //ETB=0x17 - buffer.append(0x17); - continue; - } - } else if (hex == "O") { - i++; - hex = str.mid(i, 1); - - if (hex == "T") { //EOT=0x04 - buffer.append(0x04); - continue; - } - } else if (hex == "N") { - i++; - hex = str.mid(i, 1); - - if (hex == "Q") { //ENQ=0x05 - buffer.append(0x05); - continue; - } - } else if (hex == "M") { //EM=0x19 - buffer.append(0x19); - continue; - } else if (hex == "S") { - i++; - hex = str.mid(i, 1); - - if (hex == "C") { //ESC=0x1B - buffer.append(0x1B); - continue; - } - } - } else if (letter == "A") { - i++; - hex = str.mid(i, 1); - - if (hex == "C") { - i++; - hex = str.mid(i, 1); - - if (hex == "K") { //ACK=0x06 - buffer.append(0x06); - continue; - } - } - } else if (letter == "B") { - i++; - hex = str.mid(i, 1); - - if (hex == "E") { - i++; - hex = str.mid(i, 1); - - if (hex == "L") { //BEL=0x07 - buffer.append(0x07); - continue; - } - } else if (hex == "S") { //BS=0x08 - buffer.append(0x08); - continue; - } - } else if (letter == "C") { - i++; - hex = str.mid(i, 1); - - if (hex == "R") { //CR=0x0D - buffer.append(0x0D); - continue; - } else if (hex == "A") { - i++; - hex = str.mid(i, 1); - - if (hex == "N") { //CAN=0x18 - buffer.append(0x18); - continue; - } - } - } else if (letter == "D") { - i++; - hex = str.mid(i, 1); - - if (hex == "L") { - i++; - hex = str.mid(i, 1); - - if (hex == "E") { //DLE=0x10 - buffer.append(0x10); - continue; - } - } else if (hex == "C") { - i++; - hex = str.mid(i, 1); - - if (hex == "1") { //DC1=0x11 - buffer.append(0x11); - continue; - } else if (hex == "2") { //DC2=0x12 - buffer.append(0x12); - continue; - } else if (hex == "3") { //DC3=0x13 - buffer.append(0x13); - continue; - } else if (hex == "4") { //DC2=0x14 - buffer.append(0x14); - continue; - } - } - } else if (letter == "F") { - i++; - hex = str.mid(i, 1); - - if (hex == "F") { //FF=0x0C - buffer.append(0x0C); - continue; - } else if (hex == "S") { //FS=0x1C - buffer.append(0x1C); - continue; - } - } else if (letter == "H") { - i++; - hex = str.mid(i, 1); - - if (hex == "T") { //HT=0x09 - buffer.append(0x09); - continue; - } - } else if (letter == "L") { - i++; - hex = str.mid(i, 1); - - if (hex == "F") { //LF=0x0A - buffer.append(0x0A); - continue; - } - } else if (letter == "G") { - i++; - hex = str.mid(i, 1); - - if (hex == "S") { //GS=0x1D - buffer.append(0x1D); - continue; - } - } else if (letter == "R") { - i++; - hex = str.mid(i, 1); - - if (hex == "S") { //RS=0x1E - buffer.append(0x1E); - continue; - } - } else if (letter == "U") { - i++; - hex = str.mid(i, 1); - - if (hex == "S") { //US=0x1F - buffer.append(0x1F); - continue; - } - } else if (letter == "V") { - i++; - hex = str.mid(i, 1); - - if (hex == "T") { //VT=0x0B - buffer.append(0x0B); - continue; - } - } else if (letter == "\\") { - //如果连着的是多个\\则对应添加\对应的16进制0x5C - buffer.append(0x5C); - continue; - } else { - //将对应的\[前面的\\也要加入 - buffer.append(0x5C); - buffer.append(letter.toLatin1()); - continue; - } - } - - buffer.append(str.mid(i, 1).toLatin1()); - - } - - return buffer; -} - QString QUIHelper::byteArrayToHexStr(const QByteArray &data) { QString temp = ""; diff --git a/nettool/api/quiwidget.h b/nettool/api/quiwidget.h index 589f8d6..3b93958 100644 --- a/nettool/api/quiwidget.h +++ b/nettool/api/quiwidget.h @@ -716,15 +716,14 @@ public: static quint16 getModbus16(quint8 *data, int len); static QByteArray getCRCCode(const QByteArray &data); - //字节数组转Ascii字符串 + //字节数组与Ascii字符串互转 + static void initAsciiStr(); static QString byteArrayToAsciiStr(const QByteArray &data); - //16进制字符串转字节数组 - static QByteArray hexStrToByteArray(const QString &str); - static char convertHexChar(char ch); + static QByteArray asciiStrToByteArray(const QString &data); - //Ascii字符串转字节数组 - static QByteArray asciiStrToByteArray(const QString &str); - //字节数组转16进制字符串 + //16进制字符串与字节数组互转 + static char hexStrToChar(char data); + static QByteArray hexStrToByteArray(const QString &data); static QString byteArrayToHexStr(const QByteArray &data); //获取保存的文件