https://blog.csdn.net/weixin_42837024/article/details/81669540
https://www.cnblogs.com/hysea2019/p/10972543.html
char get_hex_char(const 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 ch - ch;
}
void string_to_hex(const QString& str, QByteArray& send_data)
{
int hexdata,lowhexdata;
int hexdatalen = 0;
int len = str.length();
send_data.resize(len / 2);
char lstr,hstr;
for(int i = 0; i < len; )
{
hstr = str[i].toLatin1();
if( hstr == ' ' )
{
++i;
continue;
}
++i;
if( i >= len ) break;
lstr = str[i].toLatin1();
hexdata = get_hex_char(hstr);
lowhexdata = get_hex_char(lstr);
if( (hexdata == 16) || (lowhexdata == 16) )
break;
else
hexdata = hexdata*16 + lowhexdata;
++i;
send_data[hexdatalen] = (char)hexdata;
++hexdatalen;
}
send_data.resize(hexdatalen);
}