字符串转十六进制字符数组:
1 char cDataHigh,cDataLow; 2 int iSendDataSize = 0; 3 int iSize = Str.size(); 4 for(int i = 0;i<iSize-1;) 5 { 6 char tempByte_h = Str.at(i).toLatin1(); 7 if(tempByte_h == ' ')//去掉字符串内首部的空格 8 { 9 i+=1; 10 continue; 11 } 12 else 13 { 14 char tempByte_l = Str.at(i+1).toLatin1();//转化成ASCII码的形式,便于符号之间的运算 15 i+=2; 16 cDataHigh = MainWindow::convertHexFromChar(tempByte_h);//高位 17 cDataLow = MainWindow::convertHexFromChar(tempByte_l);//低位 18 char sendChar =(char)((cDataHigh<<4)+cDataLow);//组合 19 sendData[iSendDataSize]=sendChar; 20 iSendDataSize++; 21 }
static char convertHexFromChar(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);
}
}
字节数组转16进制字符串:
1 static QString ByteArrayToHexStr(QByteArray data) { 2 QString temp = ""; 3 QString hex = data.toHex(); 4 for (int i = 0; i < hex.length(); i = i + 2) { 5 temp += hex.mid(i, 2) + " "; 6 } 7 return temp.trimmed().toUpper(); 8 }