Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #59663

inconsistency in converting from/to hex

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <gandalf@shopzeus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.030
X-Spam-Evidence '*H*': 0.94; '*S*': 0.00; 'binary': 0.07; 'method.': 0.07; 'arguments': 0.09; 'clean.': 0.09; 'method:': 0.09; 'former,': 0.16; 'hex': 0.16; 'mailscanner,': 0.16; 'str,': 0.16; 'typeerror:': 0.16; '>>>': 0.22; 'import': 0.22; 'header:User- Agent:1': 0.23; 'bytes': 0.24; 'instead.': 0.24; 'questions:': 0.24; 'subject:/': 0.26; 'function': 0.29; 'scanned': 0.29; '"",': 0.31; 'subject:skip:i 10': 0.31; 'file': 0.32; 'class': 0.32; '(most': 0.33; 'subject:from': 0.34; 'case,': 0.35; 'convert': 0.35; 'but': 0.35; 'there': 0.35; 'believed': 0.36; 'should': 0.36; 'to:addr:python-list': 0.38; 'recent': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'dangerous': 0.60; 'viruses': 0.61; 'reverse': 0.68; 'obvious': 0.74; '"one': 0.84; 'it"': 0.84; 'same,': 0.91; 'str.': 0.91
Date Sat, 16 Nov 2013 23:16:58 +0100
From Laszlo Nagy <gandalf@shopzeus.com>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.1.0
MIME-Version 1.0
To python-list@python.org
Subject inconsistency in converting from/to hex
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-shopzeus-MailScanner-Information Please contact the ISP for more information
X-shopzeus-MailScanner-ID 125488895C52.AABD1
X-shopzeus-MailScanner Found to be clean
X-shopzeus-MailScanner-From gandalf@shopzeus.com
X-Spam-Status No
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2742.1384640221.18130.python-list@python.org> (permalink)
Lines 39
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1384640221 news.xs4all.nl 16000 [2001:888:2000:d::a6]:39518
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:59663

Show key headers only | View raw


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 | NextNext in thread | Find similar | Unroll thread


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