Steven D'Aprano added the comment: I'm sorry, but I believe that you have misunderstood what happens here. This has nothing to do with the hex codec, or int.to_bytes() etc. This is the standard property of byte strings in Python, that they are displayed using ASCII as much as possible.
The byte string b'\x41' is just the hex escape form for the byte string b'A' (ASCII capital A). It doesn't matter whether you use ASCII, decimal, octal or hexadecimal, you get an equal byte string: py> b'A' == bytes([65]) == b'\101' == b'\x41' True with the same internal byte value. When you print a byte string, Python prefers to display it using ASCII characters where possible regardless of whether it was constructed from backslash escapes or not. So the byte string b'\x41 A \xEF' prints as b'A A \xef' because \x41 is the ASCII character A, but \xEF has no ASCII representation so it remains in hex escape form. It doesn't matter where that byte string comes from: the hex codec, int.to_bytes, or somewhere else. I don't think this is appropriate to continue discussing this on the bug tracker. If you would like to continue the discussion, please join the python-l...@python.org mailing list, or comp.lang.python newsgroup, and I'll be happy to discuss it further there. ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue24551> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com