-
Notifications
You must be signed in to change notification settings - Fork 0
/
decimal_input.py
83 lines (57 loc) · 2.72 KB
/
decimal_input.py
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# !/usr/bin/env python3
import base64
from results import Results
class Decimal:
def decimal_to_ascii(self, input_string: str) -> str:
'''Convert the DECIMAL input string to ASCII string.'''
input_list = []
input_parts = input_string.split(' ')
for i in input_parts:
input_list.append(int(i))
ascii_result = ''.join(chr(x) for x in input_list)
return ascii_result
def decimal_to_base64(self, input_string: str) -> str:
'''Convert the DECIMAL input string to BASE64 string.'''
ascii_string = Decimal.decimal_to_ascii(self, input_string)
b64_string = base64.b64encode(str(ascii_string).encode()).decode()
return b64_string
def decimal_to_binary(self, input_string: str) -> str:
'''Convert the DECIMAL input string to BINARY string.'''
list_of_nums = input_string.split()
binary_string = ' '.join([bin(int(x))[2:].zfill(8) for x in list_of_nums])
return binary_string
def decimal_to_hexadecimal(self, input_string: str) -> str:
'''Convert the DECIMAL input string to HEXADECIMAL string.'''
list_of_nums = input_string.split()
hex_string = ''.join([hex(int(x))[2:] for x in list_of_nums]).upper()
return hex_string.upper()
def decimal_to_octal(self, input_string: str) -> str:
'''Convert the DECIMAL input string to OCTAL string.'''
octal_string = ''
while input_string > 0:
octal = str(input_string % 8) + octal
input_string = input_string // 8
return octal_string
def decimal_convert_all(self, input_string: str) -> None:
results = {}
results['type'] = 'Decimal'
results['input'] = f'{input_string}'
ascii = Decimal.decimal_to_ascii(self, input_string)
results['ASCII'] = f'{ascii}'
base64 = Decimal.decimal_to_base64(self, input_string)
results['Base64'] = f'{base64}'
binary = Decimal.decimal_to_binary(self, input_string)
results['Binary'] = f'{binary}'
hex = Decimal.decimal_to_hexadecimal(self, input_string)
results['Hexadecimal'] = f'{hex}'
octal = Decimal.decimal_to_octal(self, input_string)
results['Octal'] = f'{octal}'
return results
def print_decimal_output_panels(self,
input_string: str) -> None:
results = Decimal.decimal_convert_all(self, input_string)
Results.print_results_panels(self, results_dict=results)
def print_decimal_output_table(self,
input_string: str) -> None:
results = Decimal.decimal_convert_all(self, input_string)
Results.print_results_table(self, results_dict=results)