[Python-ideas] A way to subscript a single integer from bytes

Nick Coghlan ncoghlan at gmail.com
Tue May 1 09:58:40 EDT 2018


On 1 May 2018 at 21:30, Antoine Pitrou <solipsis at pitrou.net> wrote:

>
> Hi Ken,
>
> On Tue, 1 May 2018 19:22:52 +0800
> Ken Hilton <kenlhilton at gmail.com> wrote:
> >
> > So I'm pretty sure everyone here is familiar with how the "bytes" object
> > works in Python 3. It acts mostly like a string, with the exception that
> > 0-dimensional subscripting (var[idx]) returns an integer, not a bytes
> > object - the integer being the ordinal number of the corresponding
> > character.
> > However, 1-dimensional subscripting (var[idx1:idx2]) returns a bytes
> > object. Example:
> >
> >     >>> a = b'hovercraft'
> >     >>> a[0]
> >     104
> >     >>> a[4:8]
> >     b'rcra'
> >
> > Though this isn't exactly unexpected behavior (it's not possible to
> > accidentally do 1-dimensional subscripting and expect an integer it's a
> > different syntax), it's still a shame that it isn't possible to quickly
> and
> > easily subscript an integer out of it. Following up from the previous
> > example, The only way to get 493182234161465432041076 out of
> b'hovercraft'
> > in a single expression is as follows:
> >
> >     list(__import__('itertools').accumulate((i for i in a), lambda x,
> y: (x
> > << 8) + y))[-1]
>
> Let's see:
>
> >>> a = b'hovercraft'
> >>> int.from_bytes(a, 'big')
> 493182234161465432041076
>

It's also worth noting that if there's more than one integer of interest in
the string, than using the struct module is often going to be better than
using multiple slices and int.from_bytes calls:

    >>> import struct
    >>> data = b"hovercraft"
    >>> struct.unpack(">IIH", data)
    (1752135269, 1919119969, 26228)

(The struct module doesn't handle arbitrary length integers, but it handles
8, 16, 32, and 64 bit ones, which is enough for a lot of common use cases)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180501/0a1003eb/attachment-0001.html>


More information about the Python-ideas mailing list