Reputation: 829
I want to display file that convert to binary like binary editer.
For example, it convert PNG to 89 50 4E 47 0D 0A 1A 0A 00 00 00....
f = open(path, "r")
print f.read() # I want to convert this to binary
f.close()
Please advice me.
Upvotes: 4
Views: 20823
Reputation: 113974
To get a binary in a hex representation:
bin_data = open(path, 'rb').read()
import codecs
hex_data = codecs.encode(bin_data, "hex_codec")
If path
refers to a PNG file, then the first few bytes of bin_data
will look like \x89PNG\r\n
and the beginning of hex_data
will look like 89504e470d0a
. To format it nicely, add spaces:
import re
hex_with_spaces = re.sub('(..)', r'\1 ', hex_data)
The corresponding first few bytes of hex_with_spaces
will look like 89 50 4e 47 0d 0a
.
As an alternative to codecs
, the binascii
can be used:
import binascii
hex_data = binascii.hexlify(bin_data)
See jfs's answer for a more detailed example using binascii
.
To get a binary in a hex representation:
bin_data = open(path, 'rb').read()
hex_data = bin_data.encode('hex')
If path
refers to a PNG file, then the first few bytes of bin_data
will look like \x89PNG\r\n
and the beginning of hex_data
will look like 89504e470d0a
. To format it nicely, add spaces:
import re
hex_with_spaces = re.sub('(..)', r'\1 ', hex_data)
The corresponding first few bytes of hex_with_spaces
will look like 89 50 4e 47 0d 0a
.
Upvotes: 6
Reputation: 414825
To support both Python 2 and 3, you could use binascii.hexlify()
instead of .encode('hex')
:
#!/usr/bin/env python
"""Make a hexdump"""
import re
import sys
from binascii import hexlify
from functools import partial
def hexdump(filename, chunk_size=1<<15):
add_spaces = partial(re.compile(b'(..)').sub, br'\1 ')
write = getattr(sys.stdout, 'buffer', sys.stdout).write
with open(filename, 'rb') as file:
for chunk in iter(partial(file.read, chunk_size), b''):
write(add_spaces(hexlify(chunk)))
hexdump(sys.argv[1])
Note: the file is opened in binary mode, to avoid corrupting the data due to newline conversions enabled for text file such as '\r\n' -> '\n'
.
Upvotes: 3