On Sun, Dec 13, 2015 at 1:05 PM, KP <kai.pet...@gmail.com> wrote: > On Sunday, 13 December 2015 11:57:57 UTC-8, Laura Creighton wrote: >> In a message of Sun, 13 Dec 2015 11:45:19 -0800, KP writes: >> >Hi all, >> > >> > f = open("stairs.bin", "rb") >> > data = list(f.read(16)) >> > print data >> > >> >returns >> > >> >['=', '\x04', '\x00', '\x05', '\x00', '\x01', '\x00', '\x00', '\x00', >> >'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00'] >> > >> >The first byte of the file is 0x3D according to my hex editor, so why does >> >Python return '=' and not '\x3D'? >> > >> >As always, thanks for any help! >> >> 0x3d is the ascii code for '=' > > I am aware of that - so is the rule that non-printables are returned in hex > notation whereas printables come in their ASCII representation?
What you're seeing there is the repr() of the string. The rule is that it should be a printable representation that can be passed to eval to reconstruct the string. The printable requirement means that NUL should always come out escaped as '\x00' or perhaps '\0', but for printable bytes escaping isn't necessary. I don't know if there's a requirement that the repr of '\x3D' be '=', but it's probably the most generally useful and I doubt that any implementation would depart from that. If you specifically want hex representations, then you could use hex(ord(foo)). py> hex(ord('=')) '0x3d' -- https://mail.python.org/mailman/listinfo/python-list