Manipulating binary data in Python

前端 未结 7 1329
旧时难觅i
旧时难觅i 2021-02-07 07:16

I am opening up a binary file like so:

file = open(\"test/test.x\", \'rb\')

and reading in lines to a list. Each line looks a little like:

7条回答
  •  梦谈多话
    2021-02-07 07:51

    \xhh is the character with hex value hh. Other characters such as . and `~' are normal characters.

    Iterating on a string gives you the characters in it, one at a time.

    ord(c) will return an integer representing the character. E.g., ord('A') == 65.

    This will print the decimal numbers for each character:

    s = '\xbe\x00\xc8d\xf8d\x08\xe4.\x07~\x03\x9e\x07\xbe\x03\xde\x07\xfe\n'
    print ' '.join(str(ord(c)) for c in s)
    

提交回复
热议问题