-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbytechange.cpp
77 lines (70 loc) · 1.56 KB
/
bytechange.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "bytechange.h"
//16进制转换函数
QByteArray QString2Hex(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<len; )
{
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 == 16) || (lowhexdata == 16))
break;
else
hexdata = hexdata*16+lowhexdata;
i++;
senddata[hexdatalen] = (char)hexdata;
hexdatalen++;
}
senddata.resize(hexdatalen);
return senddata;
}
QString hexToString(QByteArray in)
{
int i;
int len = in.size();
unsigned char inChar,hi,lo;
QString s;
for (i=0; i<len; i++)
{
inChar = in.at(i);
hi = (inChar & 0xF0) >> 4;
if (hi > 9)
hi = 'A' + (hi - 0x0A);
else
hi += 0x30;
s.append(hi);
lo = inChar & 0x0F;
if (lo > 9)
lo = 'A' + (lo - 0x0A);
else
lo += 0x30;
s.append(lo);
s.append(0x20);
}
return s;
}
char 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);
}