2015-11-08 22:53:58 +00:00
|
|
|
import binascii
|
|
|
|
import os.path
|
|
|
|
import sys
|
|
|
|
|
2016-11-02 21:29:36 +00:00
|
|
|
|
2015-11-08 22:53:58 +00:00
|
|
|
def tof(filepath):
|
2016-11-02 21:26:55 +00:00
|
|
|
with open(filepath, 'r') as f:
|
|
|
|
content = f.read()
|
2016-11-02 21:28:28 +00:00
|
|
|
content = content.replace("0x", "")
|
2016-11-02 21:26:55 +00:00
|
|
|
content = content.split(',')
|
|
|
|
for i in range(len(content)):
|
2016-11-02 21:30:34 +00:00
|
|
|
if len(content[i]) == 1:
|
|
|
|
content[i] = "0" + content[i]
|
2016-11-02 21:26:55 +00:00
|
|
|
content = "".join(content)
|
2016-11-02 21:28:28 +00:00
|
|
|
with open(filepath + ".file", 'wb') as f:
|
2016-11-02 21:26:55 +00:00
|
|
|
content = f.write(content.decode("hex"))
|
2016-11-02 21:28:28 +00:00
|
|
|
print(os.path.basename(filepath) + ".file created.")
|
2016-11-02 21:26:55 +00:00
|
|
|
exit(0)
|
2015-11-08 22:53:58 +00:00
|
|
|
|
2016-11-02 21:29:36 +00:00
|
|
|
|
2015-11-08 22:53:58 +00:00
|
|
|
def toa(filepath):
|
2016-11-02 21:26:55 +00:00
|
|
|
with open(filepath, 'rb') as f:
|
|
|
|
content = f.read()
|
|
|
|
content = binascii.hexlify(content)
|
2016-11-02 21:28:28 +00:00
|
|
|
content = [content[i:i + 2] for i in range(0, len(content), 2)]
|
2016-11-02 21:26:55 +00:00
|
|
|
content = ",0x".join(content)
|
|
|
|
content = "0x" + content
|
2016-11-02 21:28:28 +00:00
|
|
|
content = content.replace("0x00", "0x0")
|
|
|
|
with open(filepath + ".array", 'w') as f:
|
2016-11-02 21:26:55 +00:00
|
|
|
content = f.write(content)
|
2016-11-02 21:28:28 +00:00
|
|
|
print(os.path.basename(filepath) + ".array created.")
|
2016-11-02 21:26:55 +00:00
|
|
|
exit(0)
|
2015-11-08 22:53:58 +00:00
|
|
|
|
2016-11-02 21:29:36 +00:00
|
|
|
|
2015-11-08 22:53:58 +00:00
|
|
|
def usage():
|
2016-11-02 21:26:55 +00:00
|
|
|
print("========================================================\n\
|
2015-11-08 22:53:58 +00: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-11-02 21:26:55 +00:00
|
|
|
exit(1)
|
2015-11-08 22:53:58 +00:00
|
|
|
|
|
|
|
if len(sys.argv) != 3:
|
2016-11-02 21:26:55 +00:00
|
|
|
usage()
|
2015-11-08 22:53:58 +00:00
|
|
|
if sys.argv[1] == "toa" and os.path.isfile(sys.argv[2]):
|
2016-11-02 21:26:55 +00:00
|
|
|
toa(sys.argv[2])
|
2015-11-08 22:53:58 +00:00
|
|
|
elif sys.argv[1] == "tof" and os.path.isfile(sys.argv[2]):
|
2016-11-02 21:26:55 +00:00
|
|
|
tof(sys.argv[2])
|
2015-11-08 22:53:58 +00:00
|
|
|
else:
|
2016-11-02 21:26:55 +00:00
|
|
|
usage()
|