17.11.13 08:31, Steven D'Aprano написав(ла):
There's already at least two ways to do it in Python 2:
py> import binascii
py> binascii.hexlify('Python')
'507974686f6e'
py> import codecs
py> codecs.encode('Python', 'hex')
'507974686f6e'
Third:
>>> import base64
>>> base64.b16encode(b'Python')
On 17/11/2013 06:31, Steven D'Aprano wrote:
I agree that its a bit of a mess. But only a little bit, and it will be
less messy by 3.5 when the codecs solution is re-introduced. Then the
codecs.encode and decode functions will be the one obvious way.
For anyone who's interested in the codecs i
On Sat, 16 Nov 2013 23:16:58 +0100, Laszlo Nagy wrote:
> Questions:
>
> * if we have bytes.fromhex() then why don't we have
> bytes_instance.tohex() ?
The Python core developers are quite conservative about adding new
methods, particularly when there is already a solution to the given
problem
On Saturday, November 16, 2013 5:16:58 PM UTC-5, Laszlo Nagy wrote:
> We can convert from hex str to bytes with bytes.fromhex class method:
>
> >>> b = bytes.fromhex("ff")
>
> But we cannot convert from hex binary:
>
> >>> b = bytes.fromhex(b"ff")
> Traceback (most recent call last):
>File
We can convert from hex str to bytes with bytes.fromhex class method:
>>> b = bytes.fromhex("ff")
But we cannot convert from hex binary:
>>> b = bytes.fromhex(b"ff")
Traceback (most recent call last):
File "", line 1, in
TypeError: must be str, not bytes
We don't have bytes_instance.tohex()