blob: 9fd250f387c78975d47f3775016839a2de99bca9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import struct
def bin_conv(data, data_type = None):
conv_data = None
#convert to binary
if type(data) == str and data_type == None:
conv_data = bytes(data, 'utf-8')
elif type(data) == int and data_type == None:
conv_data = data.to_bytes(32, 'little')
elif type(data) == bool and data_type == None:
conv_data = bytes(data)
#convert from binary
elif type(data) == bytes and data_type == str:
conv_data = data.decode()
conv_data = conv_data.rstrip('\x00')
elif type(data) == bytes and data_type == int:
conv_data = int.from_bytes(data, 'little')
elif type(data) == bytes and data_type == bool:
conv_data = bool(data[0])
elif type(data) == bytes and data_type == float:
conv_data = struct.unpack('d', data)[0]
return conv_data
|