//将一个字符串转换成十六进制
void Widget::String2Hex(QString str, QByteArray &senddata)
-
{
-
int hexdata,lowhexdata;
-
int hexdatalen = 0;
-
int len = str.length();
-
senddata.resize(len/2);
-
char lstr,hstr;
-
for(int i=0; i
-
{
-
-
hstr=str[i].toAscii();
-
if(hstr == ' ')
-
{
-
i++;
-
continue;
-
}
-
i++;
-
if(i >= len)
-
break;
-
lstr = str[i].toAscii();
-
hexdata = ConvertHexChar(hstr);
-
lowhexdata = ConvertHexChar(lstr);
-
if((hexdata == 16) || (lowhexdata == 16))
-
break;
-
else
-
hexdata = hexdata*16+lowhexdata;
-
i++;
-
senddata[hexdatalen] = (char)hexdata;
-
hexdatalen++;
-
}
-
senddata.resize(hexdatalen);
-
}
//将一个字符转换成十六进制
char Widget::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);
-
}
阅读(5724) | 评论(0) | 转发(0) |