Marc-Andre Lemburg added the comment:

On 11.09.2014 01:04, Nick Coghlan wrote:
> 
> Nick Coghlan added the comment:
> 
> Just as a recap of at least some of the *current* ways to do a bytes -> hex 
> conversion:
> 
>>>> import codecs
>>>> codecs.encode(b"abc", "hex")
> b'616263'
>>>> import binascii
>>>> binascii.hexlify(b"abc")
> b'616263'
>>>> import base64
>>>> base64.b16encode(b"abc")
> b'616263'
>>>> hex(int.from_bytes(b"abc", "big"))
> '0x616263'
>>>> hex(int.from_bytes(b"abc", "little"))
> '0x636261'
> 
> Thus, the underlying purpose of this proposal is to provide a single "more 
> obvious way to do it". As per the recent discussion on python-ideas, the 
> point where that is most useful is in debugging output.
> 
> However, rather than a new method on bytes/bytearray/memoryview for this, I 
> instead suggest it would be appropriate to extend the default handling of the 
> "x" and "X" format characters to accept arbitrary bytes-like objects. The 
> processing of these characters would be as follows:
> 
> "x": display a-f as lowercase digits
> "X": display A-F as uppercase digits
> "#": includes 0x prefix
> ".precision": chunks output, placing a space after every <precision> bytes
> ",": uses a comma as the separator, rather than a space

Hmm, but those would then work for str.format() as well, right ?

Since "x" and "X" are already used to convert numbers to hex
representation, opening these up for bytes sounds like it could
easily mask TypeErrors for cases where you really want an integer
to be formatted as hex and not bytes.

----------
nosy: +lemburg

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue9951>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to