aboutsummaryrefslogtreecommitdiff
path: root/dialoguer/binary_conversion.py
blob: 6b4dabaa4b812adab1bce24eaa604c0c67b76bea (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
25
26
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) == float and data_type == None:
		conv_data = struct.pack('d', data)
	elif type(data) == bool and data_type == None:
		conv_data = int(data).to_bytes(4, 'little')
	
	#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(int.from_bytes(data, 'little'))
	elif type(data) == bytes and data_type == float:
		conv_data = struct.unpack('d', data)[0]

	return conv_data