[Python-ideas] Stop displaying elements of bytes objects as printable ASCII characters in CPython 3

Nick Coghlan ncoghlan at gmail.com
Thu Sep 11 04:40:17 CEST 2014


On 11 September 2014 11:36, Ron Adam <ron3200 at gmail.com> wrote:
> When working with hex data, I prefer the way hex editors do it.  With pairs
> of hex digits separated by a space.
>
>      "50 79 74 68 6f 6e"    b'Python'
>
> But I'm not sure there's a way to make that work cleanly. :-/

I realised (http://bugs.python.org/issue22385) we could potentially
support that style through the string formatting syntax, using the
precision field to specify the number of "bytes per chunk", along with
a couple of the other existing formatting flags in the mini-language:

format(b"xyz", "x") -> '78797a'
format(b"xyz", "X") -> '78797A'
format(b"xyz", "#x") -> '0x78797a'

format(b"xyz", ".1x") -> '78 79 7a'
format(b"abcdwxyz", ".4x") -> '61626364 7778797a'
format(b"abcdwxyz", "#.4x") -> '0x61626364 0x7778797a'

format(b"xyz", ",.1x") -> '78,79,7a'
format(b"abcdwxyz", ",.4x") -> '61626364,7778797a'
format(b"abcdwxyz", "#,.4x") -> '0x61626364,0x7778797a'

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list