- 卡 ID 由设施代码和卡号组成;
- 设施代码代表前 3 位数字:
000
- 卡号代表最后 5 位数字:
99999
- 现在,我们应该把这些数字转换成二进制,这就是问题所在
- 设施代码必须是 8 位长的二进制文件
- 卡号必须是 16 位长的二进制
- 99999 不能用 16 位表示。
- 我现在该怎么办?
我创建了一个类,它采用任何小数并将其转换为带有相应设施代码和卡号的卡 ID:
class Wiegand {
private:
std::vector<bool> wiegandArray = std::vector<bool>(26, false);
uint8_t facilityCode;
uint16_t cardNumber;
void calculate(uint32_t dec) {
// transform dec number into binary number using single bit shift operation
// and store it in wiegandArray[]
for (int i = 24; i > 0; --i) {
wiegandArray[i] = dec & 1;
dec >>= 1;
}
// check for parity of the first 12 bits
bool even = 0;
for(int i = 1; i < 13; i++) {
even ^= wiegandArray[i];
}
// add 0 or 1 as first bit (leading parity bit - even) based on the number of 'ones' in the first 12 bits
wiegandArray[0] = even;
// check for parity of the last 12 bits
bool odd = 1;
for(int i = 13; i < 25; i++) {
odd ^= wiegandArray[i];
}
// add 0 or 1 as last bit (trailing parity bit - odd) based on the number of 'ones' in the last 12 bits
wiegandArray[25] = odd;
}
public:
Wiegand(uint32_t id) {
calculate(id);
facilityCode = id >> 16;
cardNumber = id & 0xffff;
}
//returns a 26 length std::vector<bool>
std::vector<bool> getWiegandBinary() {
return wiegandArray;
}
//returns uint8_t facilityCode
uint8_t getFacilityCode() {
return facilityCode;
}
//returns uint16_t cardNumber
uint16_t getCardNumber() {
return cardNumber;
}
//returns an 8 characters long String
String getCardID() {
String s1 = String(facilityCode);
String s2 = String(cardNumber);
String s3 = String(facilityCode);
while(s1.length() < 3) {
s1 = String(0) + s1;
}
while(s2.length() < 5) {
s2 = String(0) + s2;
}
String s3 = s1 + s2;
return s3;
}
};
问题是当您扫描卡片时,Wiegand Central 将返回卡片 ID,而不是十进制的 Wiegand 表示。所以我必须保留这种格式。我需要一些00099999
作为卡片 ID 而不是十进制表示的 hocus-pocus,如 my Wiegand class
.
十进制99999
以000000011000011010011111
24 位表示。这转化为:
- 设施代码:
00000001
或1
; - 卡号:
1000011010011111
或34463
; - 这意味着卡 ID
00134463
:。
我正在疯狂地制作一个算法来获取卡 ID00099999
并对其进行转换,以便它可以给我设施代码:000
和卡号99999
,然后将其转换为适当的 26 位韦根。
现在我认为不可能做到这一点。 你怎么看 ?
编辑:
我认为那个卡号00099999
甚至都不存在。我没有与任何卡供应商交谈,我认为我应该这样做,因为我缺少有关卡 ID 格式的重要信息。我认为卡 ID 只能作为0 到 255 范围内的设施代码和 0 到 65535 范围内的卡号的串联。所以可以存在的“最大”卡 ID 是25565535
。如果这是真的,输入应该由设施代码 (0-255) 和卡号 (0-65535) 组成,然后它们将形成卡号,我可以轻松地将其转换为 Wiegand 26 位。