From 03ba1a520dc304d31927fe0e34dc4bfe83858b75 Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Wed, 25 Jan 2017 11:53:23 +0000 Subject: [PATCH] First working version: both python2 and python3 --- memfs.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/memfs.py b/memfs.py index 7ac62b85a..fb4a3ac36 100755 --- a/memfs.py +++ b/memfs.py @@ -3,6 +3,7 @@ from __future__ import print_function import os import re import sys +import binascii assert len(sys.argv) > 2 @@ -37,10 +38,19 @@ for directory in dirs: print('static const unsigned char %s[] = {' % (name,), file=g) - with open(full) as f: + with open(full, 'rb') as f: i = 0 - for n in re.findall('..', f.read().encode("hex")): - print("0x%s," % (n,), end="", file=g) + #Python 2 + #fcont = f.read().encode("hex") + + #Python 2 and 3 + fcont = binascii.hexlify(f.read()) + + for n in range(0, len(fcont), 2): + twoChars = fcont[n:n+2] + if sys.version_info.major > 2: + twoChars = str(twoChars,'ascii') + print("0x%s," % (twoChars,), end="", file=g) i += 1 if (i % 20) == 0: print("", file=g)