-
Notifications
You must be signed in to change notification settings - Fork 1
/
UID.cpp
120 lines (106 loc) · 2.04 KB
/
UID.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "UID.h"
/*finish 2016 11 10 by abcABC123*/
UID::UID(const string strUid)
{
this->strUid=strUid;
}
UID::UID(const unsigned char * u, const int & len)//����һ�����ţ��Ϳ��ų���
{
for (int i = 0; i < len; i++)
{
Uid[i] = u[i];
}
}
UID::UID(UID & u)
{
Len = u.getLen();
for (int i = 0; i < Len; i++)
{
Uid[i] = u.getUid()[i]; //����������
}
}
UID::~UID()
{
}
void UID::setUid(const unsigned char * u, const int len)
{
string uid = convert(u,len);
int length = uid.length();
strUid = uid;
int i;
for (i = 0; i < length; i++)
{
Uid[i] = uid[i];
}
Uid[i] = '\0';
}
const unsigned char *UID::getUid()
{
return Uid;
}
string UID::getStrUid() {
return convert(this->Uid,this->getLen());
}
const int UID::getLen()
{
return Len;
}
const string UID::convert(const unsigned char* bits,int len) {
string res = "";
for (int i = 0; i < len; i++)
{
int bit = (int)bits[i] % 128;
/*
0 - 9;
10 - 99;
100 - 127;*/
//Ŀ���ַ��������ж�
res+=stringValue(bit);
}
return res;
}
const string UID::stringValue(int number) {
string res="";
string temp = "";
//������ת�����ַ���
do{
temp += number % 10 + '0';
number /= 10;
} while (0 != number);
//cout << "temp:" << temp.c_str();
//�ַ�����ת,ȡ������ԭʼ˳��
int len=temp.length();
//���볤��
switch (len) {
case 1:res+="00"; break;
case 2:res+="0"; break;
}
for (int i = len-1; i >= 0; i--) {
res+=temp[i];
}
return res;
}
void UID::setStrUid(const string uid)
{
this->strUid=uid;
}
bool operator==(UID & u1, UID & u2)
{
for (int i = 0; i < u1.Len; i++)
{
if (u1.Uid[i] != u2.Uid[i])
{
return false;
}
}
return true;
}
bool operator==(UID u, unsigned char * data)
{
for (int i = 0; i < u.Len; i++)
{
if (u.Uid[i] != data[i])
return false;
}
return true;
}