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: 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 To: "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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 ----------------------------------------=0A= > From: steve+comp.lang.python@pearwood.info=0A= > Subject: Re: How to get an integer from a sequence of bytes=0A= > Date: Mon=2C 27 May 2013 15:00:39 +0000=0A= > To: python-list@python.org=0A= >=0A= > On Mon=2C 27 May 2013 16:45:05 +0200=2C Mok-Kong Shen wrote:=0A= >=0A= >> From an int one can use to_bytes to get its individual bytes=2C but how= =0A= >> can one reconstruct the int from the sequence of bytes?=0A= >=0A= > Here's one way:=0A= >=0A= > py> n =3D 11999102937234=0A= > py> m =3D 0=0A= > py> for b in n.to_bytes(6=2C 'big'):=0A= > ... m =3D 256*m + b=0A= > ...=0A= > py> m =3D=3D n=0A= > True=0A= >=0A= >=0A= > --=0A= > Steven=0A= >=0A= > --=0A= > http://mail.python.org/mailman/listinfo/python-list=0A= =0A= Python 2 doesn't have to_bytes()! :(=0A= =0A= # Python 2=2C LSB 1st=0A= def to_lil_bytes(x):=0A= =A0=A0=A0 r =3D []=0A= =A0=A0=A0 while x !=3D 0:=0A= =A0=A0=A0=A0=A0=A0=A0 r.append(int(x & 0b11111111))=0A= =A0=A0=A0=A0=A0=A0=A0 x>>=3D 8=0A= =A0=A0=A0 return r=0A= =0A= # Python 2=2C LSB 1st=0A= def from_lil_bytes(l):=0A= =A0=A0=A0 x =3D 0=0A= =A0=A0=A0 for i in range(len(l)-1=2C -1=2C -1):=0A= =A0=A0=A0=A0=A0=A0=A0 x <<=3D 8=0A= =A0=A0=A0=A0=A0=A0=A0 x |=3D l[i]=0A= =A0=A0=A0 return x=0A= =0A= # Python 2=2C MSB 1st=0A= def to_big_bytes(x):=0A= =A0=A0=A0 r =3D []=0A= =A0=A0=A0 while x !=3D 0:=0A= =A0=A0=A0=A0=A0=A0=A0 r.insert(0=2C int(x & 0b11111111))=0A= =A0=A0=A0=A0=A0=A0=A0 x>>=3D 8=0A= =A0=A0=A0 return r=0A= =0A= # Python 2=2C MSB 1st=0A= def from_big_bytes(l):=0A= =A0=A0=A0 x =3D 0=0A= =A0=A0=A0 for i in range(len(l)):=0A= =A0=A0=A0=A0=A0=A0=A0 x <<=3D 8=0A= =A0=A0=A0=A0=A0=A0=A0 x |=3D l[i]=0A= =A0=A0=A0 return x=0A= =0A= Can it be faster? =