0

I have a .raw image file, and I'd like to use python3 to read all the data from the file and print a hex dump of this image.

If possible, i'd like it to run in the terminal window.

This is the code I have found and adapted so far:

import sys

src = sys.argv[1]


def hexdump( src, length=16, sep='.' ):
result = [];

# Python3 support
    try:
        xrange(0,1);
    except NameError:
        xrange = range;

    for i in xrange(0, len(src), length):
        subSrc = src[i:i+length];
        hexa = '';
        isMiddle = False;
        for h in xrange(0,len(subSrc)):
            if h == length/2:
                hexa += ' ';
            h = subSrc[h];
            if not isinstance(h, int):
                h = ord(h);
            h = hex(h).replace('0x','');
            if len(h) == 1:
                h = '0'+h;
            hexa += h+' ';
        hexa = hexa.strip(' ');
        text = '';
        for c in subSrc:
            if not isinstance(c, int):
                c = ord(c);
            if 0x20 <= c < 0x7F:
                text += chr(c);
            else:
                text += sep;
        result.append(('%08X:  %-'+str(length*(2+1)+1)+'s  |%s|') % (i, hexa, text));

    return '\n'.join(result);

if __name__ == "__main__":
    print(hexdump(src, length=16, sep='.'))

I've been using the command in the terminal:

python3 nameoffile.py nameofrawfile.raw

and it just gives me the hex values of the name of the raw file. I'd like it to read the raw file then give be all the data from it in hex.

Thanks.

EDIT: I'd like to use python as once the file is represented in hex values, I'd like to do further processing on it using python.

2 Answers2

1

One-liner:

$ python -c \
"import codecs; print(codecs.encode(open('file.raw', 'rb').read(), 'hex').decode())" 
Paulo Scardine
  • 73,447
  • 11
  • 124
  • 153
  • Hi sorry I should've specified, I'd like it in python code as I'd like to do some further processing of the file in python. Thanks –  Dec 29 '14 at 17:25
  • The expression inside `print(...)` returns the value you want (as a string), doesn't it? – Paulo Scardine Dec 29 '14 at 17:26
0

The problem is you pass the name of the file to hexdump() which treats it like data. The following corrects that and applies other relatively minor fixes to your code (and seems to work in my limited testing):

try:
    xrange
except NameError:  # Python3
    xrange = range

def hexdump(filename, length=16, sep='.'):
    result = []

    with open(filename, 'rb') as file:
        src = file.read()  # Read whole file into memory

    for i in xrange(0, len(src), length):
        subSrc = src[i:i+length]
        hexa = ''
        isMiddle = False;
        for h in xrange(0,len(subSrc)):
            if h == length/2:
                hexa += ' '
            h = subSrc[h]
            if not isinstance(h, int):
                h = ord(h)
            h = hex(h).replace('0x','')
            if len(h) == 1:
                h = '0'+h;
            hexa += h+' '
        hexa = hexa.strip(' ')
        text = ''
        for c in subSrc:
            if not isinstance(c, int):
                c = ord(c)
            if 0x20 <= c < 0x7F:
                text += chr(c)
            else:
                text += sep;
        result.append(('%08X:  %-'+str(length*(2+1)+1)+'s  |%s|') %
                      (i, hexa, text))

    return '\n'.join(result)

if __name__ == "__main__":
    import sys

    filename = sys.argv[1]
    print(hexdump(filename, length=16, sep='.'))
martineau
  • 119,623
  • 25
  • 170
  • 301
  • Hi there, thanks for your response it seems to make a lot more sense. I get this error however: Traceback (most recent call last): File "HexConv2.py", line 44, in print(hexdump(filename, length=16, sep='.')) File "HexConv2.py", line 10, in hexdump src = file.read() # Read whole file into memory OSError: [Errno 22] Invalid argument. Is this due to the size of the .raw file? It's around 4GB –  Dec 29 '14 at 18:12
  • The error message doesn't make sense because there's no argument being passed. I was unable to reproduce the problem with using Python 2.7.9 and 3.4.2 -- it looks like it worked without any errors being displayed. – martineau Dec 29 '14 at 18:26
  • P.S. There was one tab character in the indenting near the beginning of `hexdump()` in the code in my initial post, which I've removed in an edit. However that would have given you a different kind of error message. – martineau Dec 29 '14 at 18:29
  • I just noticed the part about the file being 4GB at the end of your original comment...yes, that could be a problem if you're using a 32-bit Python and/or OS. Besides reading the whole file into memory, the string `hexdump()` returns will be multiple times bigger than that. **Try using it on a smaller file and see what happens.** To make it work on huge files, you'll need to modify your function to read, process, and return results in manageable sized chunks. – martineau Dec 29 '14 at 20:02