Python Reading Wiegand Dropping Zeros
Solution 1:
Python will remove the front few bits if they are zero, this also applies to integers. For example
>>>a = 0003>>>a
3
>>>b = 0b0011>>>bin(b)
0b11
From what I see, all RFID's will have 10 numbers. You can make a simple program to add those numbers in and store the value as a string:
defrfid_formatter(value):
str_value = str(value)
for s inrange(10 - len(str_value)):
str_value = "0" + str_value
return str_value
Your test cases:
print rfid_formatter(120368)
print"0000120368"print rfid_formatter(4876298)
print"0004876298"
Solution 2:
As mentioned already, leading zeros are removed in binary sequences and also when you explicitly convert a string to decimal using int()
.
What hasn't been mentioned already is that, in Python 2.x, integers with leading zeros are treated as octal values.
>>>a = 0003>>>a
3
>>>a = 000127>>>a
87
Since this was causing confusion, the implicit octal conversion was removed in Python 3 and any number of leading zeros in numerical values will raise a SyntaxError
.
>>>a = 000127
File "<stdin>", line 1
a = 000127
^
SyntaxError: invalid token
>>>
You can read the rationale behind these decisions in PEP 3127.
Anyway, I mention all of this simply to arrive at an assumption: you're probably not working with octal representations. Instead, I think you're converting result
to a string in checkAccess
so you can do a string comparison. If this assumption is correct, you can simply use the string method zfill
(zero fill):
>>>str(119994).zfill(10)
'0000119994'
>>>>>>str(4876298).zfill(10)
'0004876298'
>>>
Post a Comment for "Python Reading Wiegand Dropping Zeros"