def bin2hexline(data, add_addr=True, width=<NUM_LIT:16>):
assert isinstance(data, binary_type), (<EOL>"<STR_LIT>" % (type(data), repr(data))<EOL>)<EOL>addr = <NUM_LIT:0><EOL>lines = []<EOL>run = True<EOL>line_width = <NUM_LIT:4> + (width * <NUM_LIT:3>) + <NUM_LIT:1><EOL>while run:<EOL><INDENT>if add_addr:<EOL><INDENT>line = ["<STR_LIT>" % addr]<EOL><DEDENT>else:<EOL><INDENT>line = []<EOL><DEDENT>ascii_block = "<STR_LIT>"<EOL>for i in range(width):<EOL><INDENT>b = data[addr]<EOL>if PY2:<EOL><INDENT>b = ord(b)<EOL><DEDENT>if chr(b) in string.printable:<EOL><INDENT>ascii_block += chr(b)<EOL><DEDENT>else:<EOL><INDENT>ascii_block += "<STR_LIT:.>"<EOL><DEDENT>line.append("<STR_LIT>" % b)<EOL>addr += <NUM_LIT:1><EOL>if addr >= len(data):<EOL><INDENT>run = False<EOL>break<EOL><DEDENT><DEDENT>line = "<STR_LIT:U+0020>".join(line)<EOL>line = line.ljust(line_width)<EOL>line += ascii_block<EOL>lines.append(line)<EOL><DEDENT>return lines<EOL>
Format binary data to a Hex-Editor like format...

e.g.:
with open("C:\Python27\python.exe", "rb") as f:
    data = f.read(150)

print("\n".join(bin2hexline(data, width=16)))

0000 4d 5a 90 00 03 00 00 00 04 00 00 00 ff ff 00 00 MZ..............
0016 b8 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00 ........@.......
0032 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0048 00 00 00 00 00 00 00 00 00 00 00 00 e8 00 00 00 ................
0064 0e 1f ba 0e 00 b4 09 cd 21 b8 01 4c cd 21 54 68 ........!..L.!Th
0080 69 73 20 70 72 6f 67 72 61 6d 20 63 61 6e 6e 6f is.program.canno
0096 74 20 62 65 20 72 75 6e 20 69 6e 20 44 4f 53 20 t.be.run.in.DOS.
0112 6d 6f 64 65 2e 0d 0d 0a 24 00 00 00 00 00 00 00 mode....$.......
0128 9d 68 ba 89 d9 09 d4 da d9 09 d4 da d9 09 d4 da .h..............
0144 d0 71 41 da d8 09                               .qA...