-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhex_input.py
More file actions
69 lines (57 loc) · 2.57 KB
/
hex_input.py
File metadata and controls
69 lines (57 loc) · 2.57 KB
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
# !/usr/bin/env python3
import binascii
from results import Results
from morse_code import MorseCode
class Hexadecimal:
def __init__(self, results, data_type):
self.results = results
self.data_type = data_type
def hex_to_ascii(self, input_string: str) -> str:
hex_string = input_string.replace(" ", "")
string = binascii.unhexlify(hex_string)
ascii_string = string.decode()
return ascii_string
def hex_to_base64(self, input_string: str) -> str:
hex_string = input_string.replace(" ", "")
hex_bytes = binascii.unhexlify(hex_string)
base64_string = binascii.b2a_base64(hex_bytes).decode()
return base64_string
def hex_to_binary(self, input_string: str) -> str:
ascii_string = Hexadecimal.hex_to_ascii(self, input_string)
n = [x for x in ascii_string]
binary_string = " ".join([bin(ord(x))[2:].zfill(8) for x in n])
return binary_string
def hex_to_decimal(self, input_string: str) -> str:
# Remove any spaces if present
hex_string = input_string.replace(" ", "")
# First, convert the HEX string to ASCII string
ascii_string = binascii.unhexlify(hex_string)
s = ascii_string.decode()
# Conversion to DECIMAL string from ASCII
d = [ord(i) for i in s]
decimal_string = " ".join(str(x) for x in d)
return decimal_string
def hex_to_morse_code(self, input_string: str) -> str:
hex_string = input_string.replace(" ", "")
morse_code_string = MorseCode.encode_morse_code(self,
input_string=hex_string)
return morse_code_string
def hex_convert_all(self, input_string: str) -> None:
self.results["type"] = "Hexadecimal"
self.results["user_input"] = f"{input_string}"
ascii = self.hex_to_ascii(input_string)
self.results["ASCII"] = f"{ascii}"
base64 = self.hex_to_base64(input_string)
self.results["Base64"] = f"{base64}"
binary = self.hex_to_binary(input_string)
self.results["Binary"] = f"{binary}"
decimal = self.hex_to_decimal(input_string)
self.results["Decimal"] = f"{decimal}"
# morse_code = MorseCode.hex_to_morse_code(self, input_string)
# self.results["Morse Code"] = f"{morse_code}"
return self.results
def print_hex_output(self, input_string: str) -> None:
output = self.hex_convert_all(input_string)
Results.print_results_table(self,
format=self.data_type,
results_dict=output)