Fix output file encoding with Python3

This commit is contained in:
Shahram Najm 2020-11-02 14:07:59 +00:00
parent 04eb06efb1
commit 89a176edd1
1 changed files with 4 additions and 8 deletions

View File

@ -42,10 +42,8 @@ CHUNK = 16 * 1024 * 1024 # chunk size in bytes
try: try:
str(b"\x23\x20", "ascii") str(b"\x23\x20", "ascii")
ascii = lambda x: str(x, "ascii") # Python 3 ascii = lambda x: str(x, "ascii") # Python 3
encode = lambda x: x.encode()
except: except:
ascii = lambda x: str(x) # Python 2 ascii = lambda x: str(x) # Python 2
encode = lambda x: x
def get_outfile_name(base, count): def get_outfile_name(base, count):
@ -93,7 +91,7 @@ for directory in dirs:
FILES[name] = fname FILES[name] = fname
SIZES[name] = os.path.getsize(full) SIZES[name] = os.path.getsize(full)
buffer.write(encode("const unsigned char %s[] = {" % (name,))) buffer.write("const unsigned char %s[] = {" % (name,))
with open(full, "rb") as f: with open(full, "rb") as f:
i = 0 i = 0
@ -107,14 +105,12 @@ for directory in dirs:
# e.g. 23 -> 0x23 # e.g. 23 -> 0x23
for n in range(0, len(contents_hex), 2): for n in range(0, len(contents_hex), 2):
twoChars = ascii(contents_hex[n : n + 2]) twoChars = ascii(contents_hex[n : n + 2])
buffer.write("0x%s," % (twoChars,))
buffer.write(encode("0x%s," % (twoChars,)))
i += 1 i += 1
if (i % 20) == 0: if (i % 20) == 0:
buffer.write(encode("\n")) buffer.write("\n")
buffer.write(encode("};\n")) buffer.write("};\n")
if buffer.tell() >= CHUNK: if buffer.tell() >= CHUNK:
buffer.close() buffer.close()
buffer = None buffer = None