2015-11-08 23:53:58 +01:00
|
|
|
import binascii
|
|
|
|
import os.path
|
|
|
|
import sys
|
|
|
|
|
2016-10-30 19:05:14 +01:00
|
|
|
|
2015-11-08 23:53:58 +01:00
|
|
|
def tof(filepath):
|
2016-10-30 18:44:57 +01:00
|
|
|
with open(filepath, 'r') as f:
|
|
|
|
content = f.read()
|
2016-10-30 18:57:40 +01:00
|
|
|
content = content.replace("0x", "")
|
2016-10-30 18:44:57 +01:00
|
|
|
content = content.split(',')
|
|
|
|
for i in range(len(content)):
|
|
|
|
if len(content[i]) == 1: content[i] = "0" + content[i]
|
|
|
|
content = "".join(content)
|
2016-10-30 18:57:40 +01:00
|
|
|
with open(filepath + ".file", 'wb') as f:
|
2016-10-30 18:44:57 +01:00
|
|
|
content = f.write(content.decode("hex"))
|
2016-10-30 18:57:40 +01:00
|
|
|
print(os.path.basename(filepath) + ".file created.")
|
2016-10-30 18:44:57 +01:00
|
|
|
exit(0)
|
2015-11-08 23:53:58 +01:00
|
|
|
|
2016-10-30 19:05:14 +01:00
|
|
|
|
2015-11-08 23:53:58 +01:00
|
|
|
def toa(filepath):
|
2016-10-30 18:44:57 +01:00
|
|
|
with open(filepath, 'rb') as f:
|
|
|
|
content = f.read()
|
|
|
|
content = binascii.hexlify(content)
|
2016-10-30 18:57:40 +01:00
|
|
|
content = [content[i:i + 2] for i in range(0, len(content), 2)]
|
2016-10-30 18:44:57 +01:00
|
|
|
content = ",0x".join(content)
|
|
|
|
content = "0x" + content
|
2016-10-30 18:57:40 +01:00
|
|
|
content = content.replace("0x00", "0x0")
|
|
|
|
with open(filepath + ".array", 'w') as f:
|
2016-10-30 18:44:57 +01:00
|
|
|
content = f.write(content)
|
2016-10-30 18:57:40 +01:00
|
|
|
print(os.path.basename(filepath) + ".array created.")
|
2016-10-30 18:44:57 +01:00
|
|
|
exit(0)
|
2015-11-08 23:53:58 +01:00
|
|
|
|
2016-10-30 19:05:14 +01:00
|
|
|
|
2015-11-08 23:53:58 +01:00
|
|
|
def usage():
|
2016-10-30 18:44:57 +01:00
|
|
|
print("========================================================\n\
|
2015-11-08 23:53:58 +01:00
|
|
|
#\n\
|
|
|
|
# Usage: python file-hex-array.py [action] [option]\n\
|
|
|
|
#\n\
|
|
|
|
# Arguments:\n\
|
|
|
|
# action ==> toa # convert file to array [option is file path]\n\
|
|
|
|
# tof # convert array to file [option is array file path]\n\
|
|
|
|
#\n\
|
|
|
|
# Example : python file-hex-array.py toa 1.png\n\
|
|
|
|
#\n\
|
|
|
|
========================================================")
|
2016-10-30 18:44:57 +01:00
|
|
|
exit(1)
|
2015-11-08 23:53:58 +01:00
|
|
|
|
|
|
|
if len(sys.argv) != 3:
|
2016-10-30 18:44:57 +01:00
|
|
|
usage()
|
2015-11-08 23:53:58 +01:00
|
|
|
if sys.argv[1] == "toa" and os.path.isfile(sys.argv[2]):
|
2016-10-30 18:44:57 +01:00
|
|
|
toa(sys.argv[2])
|
2015-11-08 23:53:58 +01:00
|
|
|
elif sys.argv[1] == "tof" and os.path.isfile(sys.argv[2]):
|
2016-10-30 18:44:57 +01:00
|
|
|
tof(sys.argv[2])
|
2015-11-08 23:53:58 +01:00
|
|
|
else:
|
2016-10-30 18:44:57 +01:00
|
|
|
usage()
|