-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconverter.py
More file actions
75 lines (68 loc) · 2.26 KB
/
converter.py
File metadata and controls
75 lines (68 loc) · 2.26 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
70
71
72
73
74
75
def to_printable_bytes(data: bytes) -> str:
return ' '.join(f"{b:02x}" for b in data)
def show_menu():
print("\nSelect input type:")
print("1. string")
print("2. hex")
print("3. int")
print("4. bin")
print("5. bytes")
choice = input("Enter option number (1–5): ").strip()
return choice
def handle_conversion():
choice = show_menu()
raw = input("Enter the value: ").strip()
if choice == "1": # string
data = raw.encode()
elif choice == "2": # hex
try:
data = bytes.fromhex(raw)
except ValueError:
print("❌ Invalid hex input.")
return
elif choice == "3": # int
try:
num = int(raw)
data = num.to_bytes((num.bit_length() + 7) // 8 or 1, 'big')
except ValueError:
print("❌ Invalid integer input.")
return
elif choice == "4": # bin
try:
num = int(raw, 2)
data = num.to_bytes((num.bit_length() + 7) // 8 or 1, 'big')
except ValueError:
print("❌ Invalid binary input.")
return
elif choice == "5": # bytes
try:
data = eval(raw)
if not isinstance(data, bytes):
raise ValueError
except:
print("❌ Invalid bytes input. Example: b'\\x41\\x42'")
return
else:
print("❌ Invalid option.")
return
print("\n======[ 🔍 Converted Output ]======")
try:
print(f"ASCII : {data.decode('utf-8')}")
except UnicodeDecodeError:
print("ASCII : [Non-printable or binary data]")
print(f"Hex : {data.hex()}")
print(f"Binary : {' '.join(bin(b)[2:].zfill(8) for b in data)}")
print(f"Decimal : {' '.join(str(b) for b in data)}")
print(f"Bytes : {data}")
print(f"Int (big endian) : {int.from_bytes(data, 'big')}")
print(f"Int (little endian): {int.from_bytes(data, 'little')}")
print("===================================")
def from_input():
while True:
handle_conversion()
again = input("\nDo you want to convert another input? (y/n): ").strip().lower()
if again != 'y':
print("Bye 👋")
break
if __name__ == "__main__":
from_input()