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


Groups > comp.lang.python > #46237

RE: How to get an integer from a sequence of bytes

Path csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <carlosnepomuceno@outlook.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.046
X-Spam-Evidence '*H*': 0.91; '*S*': 0.00; 'bytes,': 0.09; 'way:': 0.09; 'subject:How': 0.10; 'python': 0.11; 'def': 0.12; '-1):': 0.16; '-1,': 0.16; 'wrote:': 0.18; '+0000': 0.22; 'to:name:python- list@python.org': 0.22; 'bytes': 0.24; 'integer': 0.24; 'received:65.55.116': 0.24; 'mon,': 0.24; 'header:In-Reply-To:1': 0.27; 'skip:- 40': 0.29; "doesn't": 0.30; 'url:mailman': 0.30; 'steven': 0.31; 'url:python': 0.33; 'date:': 0.34; 'subject:from': 0.34; 'but': 0.35; '+0200,': 0.36; 'sequence': 0.36; 'url:listinfo': 0.36; 'url:org': 0.36; 'email addr:python.org': 0.37; 'to:addr:python-list': 0.38; 'subject:': 0.39; '\xa0\xa0\xa0': 0.39; 'to:addr:python.org': 0.39; 'url:mail': 0.40; 'how': 0.40; 're:': 0.63; 'skip:n 10': 0.64; 'email name :python-list': 0.65; '1st': 0.74; 'subject:get': 0.81; '2013': 0.98
X-TMN [/g3GncAYs+JCqSsz0WvXaU80G3kMpgvo]
X-Originating-Email [carlosnepomuceno@outlook.com]
From Carlos Nepomuceno <carlosnepomuceno@outlook.com>
To "python-list@python.org" <python-list@python.org>
Subject RE: How to get an integer from a sequence of bytes
Date Tue, 28 May 2013 03:37:37 +0300
Importance Normal
In-Reply-To <51a37516$0$30002$c3e8da3$5496439d@news.astraweb.com>
References <51a37516$0$30002$c3e8da3$5496439d@news.astraweb.com>
Content-Type text/plain; charset="iso-8859-1"
Content-Transfer-Encoding quoted-printable
MIME-Version 1.0
X-OriginalArrivalTime 28 May 2013 00:37:37.0552 (UTC) FILETIME=[8D805900:01CE5B3B]
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 <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2270.1369701466.3114.python-list@python.org> (permalink)
Lines 64
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1369701466 news.xs4all.nl 15885 [2001:888:2000:d::a6]:44836
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:46237

Show key headers only | View raw


----------------------------------------
> From: steve+comp.lang.python@pearwood.info
> Subject: Re: How to get an integer from a sequence of bytes
> Date: Mon, 27 May 2013 15:00:39 +0000
> To: python-list@python.org
>
> On Mon, 27 May 2013 16:45:05 +0200, Mok-Kong Shen wrote:
>
>> From an int one can use to_bytes to get its individual bytes, but how
>> can one reconstruct the int from the sequence of bytes?
>
> Here's one way:
>
> py> n = 11999102937234
> py> m = 0
> py> for b in n.to_bytes(6, 'big'):
> ... m = 256*m + b
> ...
> py> m == n
> True
>
>
> --
> Steven
>
> --
> http://mail.python.org/mailman/listinfo/python-list

Python 2 doesn't have to_bytes()! :(

# Python 2, LSB 1st
def to_lil_bytes(x):
    r = []
    while x != 0:
        r.append(int(x & 0b11111111))
        x>>= 8
    return r

# Python 2, LSB 1st
def from_lil_bytes(l):
    x = 0
    for i in range(len(l)-1, -1, -1):
        x <<= 8
        x |= l[i]
    return x

# Python 2, MSB 1st
def to_big_bytes(x):
    r = []
    while x != 0:
        r.insert(0, int(x & 0b11111111))
        x>>= 8
    return r

# Python 2, MSB 1st
def from_big_bytes(l):
    x = 0
    for i in range(len(l)):
        x <<= 8
        x |= l[i]
    return x

Can it be faster? 		 	   		  

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How to get an integer from a sequence of bytes Mok-Kong Shen <mok-kong.shen@t-online.de> - 2013-05-27 16:45 +0200
  Re: How to get an integer from a sequence of bytes Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-27 15:00 +0000
    RE: How to get an integer from a sequence of bytes Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-28 03:37 +0300
  Re: How to get an integer from a sequence of bytes Ned Batchelder <ned@nedbatchelder.com> - 2013-05-27 11:30 -0400
    Re: How to get an integer from a sequence of bytes Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-28 00:31 +0000
      Re: How to get an integer from a sequence of bytes Dave Angel <davea@davea.name> - 2013-05-27 20:41 -0400
    Re: How to get an integer from a sequence of bytes Mok-Kong Shen <mok-kong.shen@t-online.de> - 2013-05-30 20:26 +0200
      Re: How to get an integer from a sequence of bytes Ian Kelly <ian.g.kelly@gmail.com> - 2013-05-30 12:42 -0600
        Re: How to get an integer from a sequence of bytes jmfauth <wxjmfauth@gmail.com> - 2013-05-30 11:53 -0700
      Re: How to get an integer from a sequence of bytes Ned Batchelder <ned@nedbatchelder.com> - 2013-05-30 15:22 -0400
        Re: How to get an integer from a sequence of bytes Mok-Kong Shen <mok-kong.shen@t-online.de> - 2013-06-02 21:25 +0200
          Re: How to get an integer from a sequence of bytes Chris Angelico <rosuav@gmail.com> - 2013-06-03 05:54 +1000
          Re: How to get an integer from a sequence of bytes Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-06-02 21:48 -0400
            Re: How to get an integer from a sequence of bytes Grant Edwards <invalid@invalid.invalid> - 2013-06-03 14:31 +0000
              Re: How to get an integer from a sequence of bytes Dave Angel <d@davea.name> - 2013-06-03 18:07 -0400
                Re: How to get an integer from a sequence of bytes Grant Edwards <invalid@invalid.invalid> - 2013-06-03 22:34 +0000
              Re: How to get an integer from a sequence of bytes Dan Stromberg <drsalists@gmail.com> - 2013-06-03 15:41 -0700
                Re: How to get an integer from a sequence of bytes Grant Edwards <invalid@invalid.invalid> - 2013-06-04 13:39 +0000
                Re: How to get an integer from a sequence of bytes Joshua Landau <joshua.landau.ws@gmail.com> - 2013-06-04 20:51 +0100
                Re: How to get an integer from a sequence of bytes Chris Angelico <rosuav@gmail.com> - 2013-06-05 07:49 +1000
                Re: How to get an integer from a sequence of bytes Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-06-04 20:34 -0400
                Re: How to get an integer from a sequence of bytes Tim Roberts <timr@probo.com> - 2013-06-04 22:11 -0700
                Re: How to get an integer from a sequence of bytes Fábio Santos <fabiosantosart@gmail.com> - 2013-06-12 15:00 +0100
                Re: How to get an integer from a sequence of bytes Grant Edwards <invalid@invalid.invalid> - 2013-06-12 14:42 +0000
              RE: How to get an integer from a sequence of bytes Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-06-04 02:18 +0300
                Re: How to get an integer from a sequence of bytes Grant Edwards <invalid@invalid.invalid> - 2013-06-04 13:42 +0000
                RE: How to get an integer from a sequence of bytes Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-06-04 16:58 +0300
              Re: How to get an integer from a sequence of bytes Dan Stromberg <drsalists@gmail.com> - 2013-06-03 16:47 -0700
  Re: How to get an integer from a sequence of bytes Grant Edwards <invalid@invalid.invalid> - 2013-05-28 16:04 +0000

csiph-web