Skip to content

Latest commit

 

History

History
26 lines (22 loc) · 994 Bytes

十六进制字符串转换.md

File metadata and controls

26 lines (22 loc) · 994 Bytes

十六进制字符串转换

/* ascii to hexString 将ascii字符串转换成hex字符串 "35"->"33 35" */
QString SerialAssistant::asciiToHexString(QString src)
{
    QByteArray buf;
    buf = src.toLocal8Bit();    // "35" -> 0x33 0x35
    buf = buf.toHex().toUpper();// 0x33 0x35 -> 0x33 0x35
    buf = this->insertSpace(buf );
    return QString::fromLocal8Bit(buf);// 0x33 0x35 -> "33 35"
}
/* hexString to ascii 将hex字符串转换成ascii字符串 "33 35"->"35" */
QString SerialAssistant::hexStringToAscii(QString src)
{
    QByteArray buf = stringToSend(src ,true);// "33 35"   -> 0x33 0x35
    return QString::fromLocal8Bit(buf);      // 0x33 0x35 -> "35"
}

/* asciiString to hex  将ascii字符串转换成hex字符串 "35"-> 0x35*/
QByteArray txBuf = QByteArray::fromHex(str.toLocal8Bit());

/* hex to asciiString  将ascii字符串转换成hex字符串 0x35 0x36-> "35 36"*/
QString str =  txBuf.toHex(' ').toUpper; // 0x35 0x36-> "35 36"