16进制字符串转QByteArray,char转16进制字符串

直接上代码,看代码你们就懂了

1、16进制QString转QByteArray

 QString str = "01 a5 1e 02";
QByteArray tmpBy;
StringToHex(str, tmpBy); void MainWindow::StringToHex(QString str, QByteArray &senddata)
{
int hexdata,lowhexdata;
int hexdatalen = ;
int len = str.length();
senddata.resize(len/);
char lstr,hstr;
for(int i=; i<len; )
{
//char lstr,
hstr=str[i].toLatin1();
if(hstr == ' ')
{
i++;
continue;
}
i++;
if(i >= len)
break;
lstr = str[i].toLatin1();
hexdata = ConvertHexChar(hstr);
lowhexdata = ConvertHexChar(lstr);
if((hexdata == ) || (lowhexdata == ))
break;
else
hexdata = hexdata*+lowhexdata;
i++;
senddata[hexdatalen] = (char)hexdata;
hexdatalen++;
}
senddata.resize(hexdatalen);
} char MainWindow::ConvertHexChar(char ch)
{
if((ch >= '') && (ch <= ''))
return ch-0x30;
else if((ch >= 'A') && (ch <= 'F'))
return ch-'A'+;
else if((ch >= 'a') && (ch <= 'f'))
return ch-'a'+;
else return ch-ch;//不在0-f范围内的会发送成0
}

2、qint8(signed char)转16进制字符串

     QDataStream out(&requestData,QIODevice::ReadWrite);    //将字节数组读入
QString strReceive;
int index = ;
while(!out.atEnd())
{
qint8 outChar = ;
out>>outChar; //每字节填充一次,直到结束
//十六进制的转换
QString str = QString("%1").arg(outChar & 0xFF,,,QLatin1Char(''));
strReceive.append(str);
}

3、这块我主要是用在串口通信这里了,看自己实际情况吧

上一篇:Appium 九宫格 手势解锁


下一篇:vue中生成二维码