add BCD decoders
This commit is contained in:
parent
10830999be
commit
ee1c52a1c7
1 changed files with 19 additions and 0 deletions
19
dcd/bit.py
19
dcd/bit.py
|
@ -56,3 +56,22 @@ def hex_to_raw(text, separator=" "):
|
|||
output.append(int(text[2*i:2*i+2], 16))
|
||||
|
||||
return bytearray(output)
|
||||
|
||||
def raw_to_bcd(raw):
|
||||
"""
|
||||
Decodes a bytearray (or bytes) to an integer as BCD.
|
||||
"""
|
||||
|
||||
sum = 0
|
||||
order = 1
|
||||
|
||||
for byte in reversed(raw):
|
||||
sum += (byte & 0x0f) * order
|
||||
order *= 10
|
||||
sum += (byte >> 4) * order
|
||||
order *= 10
|
||||
|
||||
return sum
|
||||
|
||||
def hex_to_bcd(text):
|
||||
return raw_to_bcd(hex_to_raw(text))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue