-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhexstring_to_ascii.py
46 lines (40 loc) · 1.28 KB
/
hexstring_to_ascii.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
#!/usr/bin/python3
import sys
import os
def script_path():
path = os.path.realpath(os.path.dirname(sys.argv[0]))
os.chdir(path) #it seems to be quite important
return path
def chunks(l, n):
some = [(l[i:i + n]) for i in range(0, len(l), n)]
return some
def hex_ascii(hexString):
if " " in hexString:
hexes = hexString.split()
else:
hexes = chunks(hexString, 2)
return "".join([chr(int("0x"+item,16)) for item in hexes])
def ascii_hex(asciiString):
hexString = [("0"*(4-len(hex(ord(item)))) + hex(ord(item))[2:]).upper() for item in asciiString]
prefix = " ".join(["0x" + item for item in hexString])
return " ".join(hexString), "".join(hexString), prefix
if __name__ == "__main__":
ask = input("hex_to_ascii(1) or ascii_to_hex(2)?\n")
if ask == "1":
hexToAscii = True
elif ask == "2":
hexToAscii = False
else:
print("wrong choice. Exiting...")
sys.exit()
if hexToAscii:
some = input("podaj hexstring:\n")
else:
some = input("podaj asciistring:\n")
while some != "":
if hexToAscii:
print(hex_ascii(some))
some = input("podaj hexstring:\n")
else:
print(ascii_hex(some))
some = input("podaj asciistring:\n")