-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.cpp
More file actions
88 lines (79 loc) · 1.72 KB
/
common.cpp
File metadata and controls
88 lines (79 loc) · 1.72 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
#include "common.h"
#include <string>
#include <cctype>
#include <QTextCodec>
std::string strToHex(const std::string &str)
{
std::string temp;
for(decltype(str.size()) count=0;count!=str.size()&&count!=str.size()-1;count+=2)
{
int high;
int low;
char strTemp=toupper(str[count]);
if(strTemp>='0'&&strTemp<='9')
{
high=strTemp-48;
}
else if(strTemp>='A'&&strTemp<='F')
{
high=strTemp-55;
}
else
{
return temp;
}
strTemp=toupper(str[count+1]);
if(strTemp>='0'&&strTemp<='9')
{
low=strTemp-48;
}
else if(strTemp>='A'&&strTemp<='F')
{
low=strTemp-55;
}
else
{
return temp;
}
temp.push_back(high<<4|low);
}
return temp;
}
QString hexToStr(const unsigned char *data)
{
QString result;
unsigned char temp;
while(*data!='\0')
{
temp=*data>>4;
if(temp>=0x0A&&temp<=0x0F)
{
temp+=55;
}
else
{
temp+=48;
}
result+=static_cast<char>(temp);
temp=*data&0x0F;
if(temp>=0x0A&&temp<=0x0F)
{
temp+=55;
}
else
{
temp+=48;
}
result+=static_cast<char>(temp);
result+=' ';
data++;
}
return result;
}
QByteArray transcoding(QByteArray str,const char *srcCode,const char *dstCode)
{
QTextCodec *codec=QTextCodec::codecForName(srcCode);
QString temp=codec->toUnicode(str);
QTextCodec *decodec=QTextCodec::codecForName(dstCode);
return (decodec->fromUnicode(temp));
}