Search code examples
pythonformathexbinord

Python binary to multi- hex


I am trying to read a file in binary and return for example "ffffff" a series of 6 hex codes. does this make sense? The code I have (below) only returns a list of 2 so it looks like "ff"

fp = open(f, 'rb')
hex_list = ("{:02x}".format(ord(c)) for c in fp.read())

i am specifically looking to make this return something like

['ab0012', 'ffbaf0']

not like

['ab', '00', '12', 'ff', 'ba', 'f0']

any help would be appreciated thanks.


Solution

  • How about this:

    fp = open(f, 'rb')
    hex_list = ["{:02x}".format(ord(c)) for c in fp.read()]
    return [''.join(hex_list[n:n+3]) for n in range(0, len(hex_list), 3)]