Skip to content

Added filter option to the 'hex2bin' script and function #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions intelhex/__init__.py
Original file line number Diff line number Diff line change
@@ -1035,7 +1035,7 @@ def tobinarray(self, start=None, end=None, size=None):
#/class IntelHex16bit


def hex2bin(fin, fout, start=None, end=None, size=None, pad=None):
def hex2bin(fin, fout, start=None, end=None, size=None, pad=None, filter=False):
"""Hex-to-Bin convertor engine.
@return 0 if all OK

@@ -1045,6 +1045,7 @@ def hex2bin(fin, fout, start=None, end=None, size=None, pad=None):
@param end end of address range (inclusive; optional)
@param size size of resulting file (in bytes) (optional)
@param pad padding byte (optional)
@param filter whether to filter the input range (optional)
"""
try:
h = IntelHex(fin)
@@ -1070,7 +1071,10 @@ def hex2bin(fin, fout, start=None, end=None, size=None, pad=None):
if pad is not None:
# using .padding attribute rather than pad argument to function call
h.padding = pad
h.tobinfile(fout, start, end)
if filter:
h[start:end+1].tobinfile(fout)
else:
h.tobinfile(fout, start, end)
except IOError:
e = sys.exc_info()[1] # current exception
txt = "ERROR: Could not write to file: %s: %s" % (fout, str(e))
34 changes: 26 additions & 8 deletions intelhex/scripts/hex2bin.py
Original file line number Diff line number Diff line change
@@ -37,6 +37,18 @@

VERSION = '2.3.0'

def split_range(a):
'''Split a range string "START:END" into integer parts
@param a string in the form "START:END"
@return start,end start and end values as integers
'''
l = a.split(":")
if l[0] != '':
start = int(l[0], 16)
if l[1] != '':
end = int(l[1], 16)
return start, end

def main():
import getopt
import os
@@ -58,6 +70,9 @@ def main():
-r, --range=START:END specify address range for writing output
(ascii hex value).
Range can be in form 'START:' or ':END'.
-f, --filter=START:END specify address range for filtering input
(ascii hex value).
Filter range can be in form 'START:' or ':END'.
-l, --length=NNNN,
-s, --size=NNNN size of output (decimal value).
'''
@@ -66,11 +81,12 @@ def main():
start = None
end = None
size = None
filter = False

try:
opts, args = getopt.getopt(sys.argv[1:], "hvp:r:l:s:",
opts, args = getopt.getopt(sys.argv[1:], "hvp:r:f:l:s:",
["help", "version", "pad=", "range=",
"length=", "size="])
"filter=", "length=", "size="])

for o, a in opts:
if o in ("-h", "--help"):
@@ -86,13 +102,15 @@ def main():
raise getopt.GetoptError('Bad pad value')
elif o in ("-r", "--range"):
try:
l = a.split(":")
if l[0] != '':
start = int(l[0], 16)
if l[1] != '':
end = int(l[1], 16)
start, end = split_range(a)
except:
raise getopt.GetoptError('Bad range value(s)')
elif o in ("-f", "--filter"):
filter = True
try:
start, end = split_range(a)
except:
raise getopt.GetoptError('Bad filter range value(s)')
elif o in ("-l", "--lenght", "-s", "--size"):
try:
size = int(a, 10)
@@ -129,7 +147,7 @@ def main():
fout = compat.get_binary_stdout()

from intelhex import hex2bin
sys.exit(hex2bin(fin, fout, start, end, size, pad))
sys.exit(hex2bin(fin, fout, start, end, size, pad, filter))

if __name__ == '__main__':
main()