Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #59663
| Date | 2013-11-16 23:16 +0100 |
|---|---|
| From | Laszlo Nagy <gandalf@shopzeus.com> |
| Subject | inconsistency in converting from/to hex |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2742.1384640221.18130.python-list@python.org> (permalink) |
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 "<stdin>", line 1, in <module>
TypeError: must be str, not bytes
We don't have bytes_instance.tohex() instance method.
But we have binascii.hexlify. But binascii.hexlify does not return an
str. It returns a bytes instance instead.
>>> import binascii
>>> binascii.hexlify(b)
b'ff'
Its reverse function binascii.unhexlify can be used on str and bytes too:
>>> binascii.unhexlify(b'ff')
b'\xff'
>>> binascii.unhexlify('ff')
b'\xff'
Questions:
* if we have bytes.fromhex() then why don't we have bytes_instance.tohex() ?
* if the purpose of binascii.unhexlify and bytes.fromhex is the same,
then why allow binary arguments for the former, and not for the later?
* in this case, should there be "one obvious way to do it" or not?
--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
inconsistency in converting from/to hex Laszlo Nagy <gandalf@shopzeus.com> - 2013-11-16 23:16 +0100
Re: inconsistency in converting from/to hex Ned Batchelder <ned@nedbatchelder.com> - 2013-11-16 15:35 -0800
Re: inconsistency in converting from/to hex Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-17 06:31 +0000
Re: inconsistency in converting from/to hex Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-17 12:38 +0000
Re: inconsistency in converting from/to hex Serhiy Storchaka <storchaka@gmail.com> - 2013-11-17 18:22 +0200
csiph-web