Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Read hex from file into list?

Tags:

python

hex

Is there a simple way to, in Python, read a file's hexadecimal data into a list, say hex?

So hex would be this:

hex = ['AA','CD','FF','0F']

I don't want to have to read into a string, then split. This is memory intensive for large files.

like image 254
Joseph Avatar asked Feb 19 '16 22:02

Joseph


People also ask

How do you read bytes as hex in python?

Use the hex() Method to Convert a Byte to Hex in Python The hex() method introduced from Python 3.5 converts it into a hexadecimal string. In this case, the argument will be of a byte data type to be converted into hex.


2 Answers

s = "Hello"
hex_list = ["{:02x}".format(ord(c)) for c in s]

Output

['48', '65', '6c', '6c', '6f']

Just change s to open(filename).read() and you should be good.

with open('/path/to/some/file', 'r') as fp:
    hex_list = ["{:02x}".format(ord(c)) for c in fp.read()]

Or, if you do not want to keep the whole list in memory at once for large files.

hex_list = ("{:02x}".format(ord(c)) for c in fp.read())

and to get the values, keep calling

next(hex_list)

to get all the remaining values from the generator

list(hex_list)
like image 188
OneCricketeer Avatar answered Oct 22 '22 13:10

OneCricketeer


Using Python 3, let's assume the input file contains the sample bytes you show. For example, we can create it like this

>>> inp = bytes((170,12*16+13,255,15)) # i.e. b'\xaa\xcd\xff\x0f'
>>> with open(filename,'wb') as f:
...     f.write(inp)

Now, given we want the hex representation of each byte in the input file, it would be nice to open the file in binary mode, without trying to interpret its contents as characters/strings (or we might trip on the error UnicodeDecodeError: 'utf-8' codec can't decode byte 0xaa in position 0: invalid start byte)

>>> with open(filename,'rb') as f:
...     buff = f.read() # it reads the whole file into memory
...
>>> buff
b'\xaa\xcd\xff\x0f'
>>> out_hex = ['{:02X}'.format(b) for b in buff]
>>> out_hex
['AA', 'CD', 'FF', '0F']

If the file is large, we might want to read one character at a time or in chunks. For that purpose I recommend to read this Q&A

like image 8
Pynchia Avatar answered Oct 22 '22 12:10

Pynchia