-
Notifications
You must be signed in to change notification settings - Fork 3
/
ascii2hex.py
executable file
·75 lines (66 loc) · 2.43 KB
/
ascii2hex.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
#!/usr/bin/env python3
import binascii
import sys
from sys import stdout
import os
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-x', '--hex', action='store', dest='x', help='Hex that you would like to convert to ascii', default='')
parser.add_argument('-a', '--ascii', action='store', dest='a', help='Ascii that you would like to convert to hex', default='')
args = parser.parse_args()
def Banner():
os.system('clear')
print ("+==================================================================================================================================================")
print ("|")
print ("| Title: %s" % sys.argv[0])
print ("| Author: glyph")
print ("| Creation Date: 21/02/2020")
print (f"| Usage: {sys.argv[0]} '-x' \'<hex to convert to ascii>\' OR {sys.argv[0]} -a' \'<ascii to convert to hex>\'")
print ("| Description: This takes either a ASCII string or HEX Value set, with the opposite result as output.")
print ("|")
print ("+==================================================================================================================================================")
def Usage():
print (f"Usage: {sys.argv[0]} '-x' \'<hex to convert to ascii>\' OR {sys.argv[0]} -a' \'<ascii to convert to hex>\'")
def asciiPrint(string):
hexarray = []
print ("[Original String]\n\n\'%s\'" % string)
for char in string:
y = binascii.hexlify(bytearray(char, 'utf-8'))
hexarray.append(y)
print ("\n[Hex Conversion]\n")
for i in hexarray:
print ("\\x" + i.decode('utf-8'), end='')
print ("\n")
def hexPrint(string):
hexarray = []
split = []
if string[0:2] == "\\x":
split = string.split("\\x")
elif string[1] == " ":
split = string.split(" ")
else:
i = 0
string_spaced = ''
for char in string:
string_spaced += string[i:i+2] + " "
i += 1
split = string.split(" ")
for char in split:
if char == '':
pass
else:
y = binascii.unhexlify(char)
hexarray.append(y)
print ("[Original Hex]\n\n\'%s\'" % string)
print ("\n[Ascii Conversion]\n")
for i in hexarray:
print (i.decode('latin-1'), end='')
print ("\n")
if __name__ == "__main__":
Banner()
if (args.x and args.a):
Usage()
elif args.x:
hexPrint(args.x)
elif args.a:
asciiPrint(args.a)