Skip to content

Instantly share code, notes, and snippets.

@hhc0null
Last active December 10, 2015 18:38
Show Gist options
  • Save hhc0null/4475624 to your computer and use it in GitHub Desktop.
Save hhc0null/4475624 to your computer and use it in GitHub Desktop.
hex2asc.py 0.0.1, a converter to ascii characters from hex strings. Basic Usage: $ hex2asc.py --space 41 41 41 AAA $ hex2asc.py 414141 AAA --- i have to finish a repot of phisics...
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import sys
argc = len(sys.argv)
argv = sys.argv
def ShowUsage():
print """
hex2asc.py 0.0.1, a converter to ascii characters from hex strings.
Basic Usage:
$ hex2asc.py
[Show this message.]
$ hex2asc.py --help
[Show this message.]
$ hex2asc.py --space 41 41 41
AAA
$ hex2asc.py 414141
AAA
"""
exit(0)
def ShowError(mes):
print "Error: "+mes
exit(-1)
def IsRangeOfASCIIChar(ch):
if (0x20 <= int(ch, 16)) and (int(ch, 16) <= 0x7e):
return chr(int(ch, 16))
else:
return ""
l = []
if argc < 2:
ShowUsage()
if argv[1] == "--space":
for i in argv[2:]:
try:
l += IsRangeOfASCIIChar(i)
except ValueError:
ShowError('ValueError')
elif argv[1] == "--help":
ShowUsage()
else:
str = argv[1]
if not len(str)%2:
length = len(str)/2
else:
print "Error: argument is not even number"
sys.exit(-1)
for i in range(length):
tmp = str[0+i*2:2+i*2]
try:
l += IsRangeOfASCIIChar(tmp)
except ValueError:
ShowError('ValueError')
except TypeError:
ShowError('TypeError')
if l == []:
print "Aborting..."
sys.exit(-1)
print "".join(l)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment