2

I am trying to program a hex-viewer for cmd in python 2.7
It works almost fine, but if I try to view a compiled file on windows, it shows only a little part of it. I already figured out, that read() breaks at the first occurrence of 0x1a (in ASCII). Notepad++ shows this char as SUB. I don't know what this control-character does, why read() stops at this char and how to avoid this break. Can anyone help me?

Here's my whole code:

    def main():

        while True:
            print "Enter a file path:"
            path = raw_input()
            f = open(path, 'r')
            text = f.read() # seems to break at 0x1a/SUB
            f.close()
            for c in text:
                hex_c = hex(ord(c))[2:]
                if len(hex_c) % 2: # if the hex number consists of 1 digit
                    hex_c = '0' + hex_c # fill the string with a zero
                print hex_c,
            print # just as a line break in the console

    if __name__ == '__main__':

        main()

1 Answer 1

2

f = open(path, 'r') opens the file in text mode.

While it has little importance on Linux, if you stil use Python 2.x, on Windows, text mode enables end of line conversion (CRLF become LF aka \r\n become \n aka 0x0D 0x0A becomes 0x0A)

I must admit I cannot explain why you have this behaviour, but for a hex editor, you have open the file as binary or you'll lose all 0x0d bytes (and other surprises I'm apparently unaware of, I'll make some more research):

f = open(path, 'rb')

no conversion is performed, file is accessed in raw mode, I cannot see how it wouldn't fix your problem.

(also don't forget to f.close() your file, as it's currently not done, or use a with open(path,"rb") as f: statement.

BTW: direct 2-digit hex can be achieved by: hex_c = "%02x" % ord(c)

EDIT: I tried with python 3, and it won't even let me read a binary file as text. I got UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 46: character maps to <undefined>. At least you can't do it from the start!

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! I am actually closing the file directly after the read-call, I just forgot to type the code line here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.