forked from saltastro/timDIMM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinutils.py
More file actions
26 lines (22 loc) · 722 Bytes
/
binutils.py
File metadata and controls
26 lines (22 loc) · 722 Bytes
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
'''misc routines for dealing with binary data'''
def hex2bin(str):
'''
take a hexadecimal number as a string and convert it to a binary string
'''
bin = ['0000', '0001', '0010', '0011',
'0100', '0101', '0110', '0111',
'1000', '1001', '1010', '1011',
'1100', '1101', '1110', '1111']
aa = ''
for i in range(len(str)):
aa += bin[int(str[i], base=16)]
return aa
def checksum(str):
'''twos complement checksum as used by the ox wagon PLC'''
command = str[1:len(str) - 4]
sum = 0
for i in range(0, len(command), 2):
byte = command[i] + command[i + 1]
sum = sum + int(byte, base=16)
neg = ~sum & 0xFF
return neg + 1