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

2013-06-13 Thread Tim Roberts
Fábio Santos  wrote:

>On 5 Jun 2013 06:23, "Tim Roberts"  wrote:
>> A single machine word was 60 bits, so a single register read got you 10
>> characters.
>
>10 characters! Now that sounds like it's enough to actually store a word.
>However long words can inadverten be cropped.

Well, Cybers weren't often used for heavy text processing.  Most of the
time, a "word" was limited to 7 characters, so you could fit an 18-bit
address in the bottom of the register.
-- 
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-12 Thread Grant Edwards
On 2013-06-12, F?bio Santos  wrote:
> On 5 Jun 2013 06:23, "Tim Roberts"  wrote:
>> A single machine word was 60 bits, so a single register read got you 10
>> characters.
>
> 10 characters! Now that sounds like it's enough to actually store a word.
> However long words can inadverten be cropped.

Only the first 10 characters in variable names were significant when
you wrote Pascal programs (I don't remember if that was true for
FORTRAN, but I bet it was).

-- 
Grant Edwards   grant.b.edwardsYow! I'm gliding over a
  at   NUCLEAR WASTE DUMP near
  gmail.comATLANTA, Georgia!!
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-12 Thread Fábio Santos
On 5 Jun 2013 06:23, "Tim Roberts"  wrote:
> A single machine word was 60 bits, so a single register read got you 10
> characters.

10 characters! Now that sounds like it's enough to actually store a word.
However long words can inadverten be cropped.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-12 Thread Ian Kelly
On Tue, Jun 4, 2013 at 3:49 PM, Chris Angelico  wrote:
> So... can we cite http://xkcd.com/859/ in two threads at once, or does
> that create twice as much tension?

No, you just look at one of them upside-down, and then they cancel
each other out.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-04 Thread Tim Roberts
Grant Edwards  wrote:

>On 2013-06-03, Dan Stromberg  wrote:
>>
>> When I was a Freshman in college, I used a CDC Cyber a lot; it had 6 bit
>> bytes and 60 bit words.  This was in 1985.
>
>But you couldn't address individual 6-bit "hextets" in memory could
>you?  My recollection is that incrementing a memory address got you
>the next 60-bit chunk -- that means that by the older terminology a
>"byte" was 60 bits.  A "character" was 6 bits, and a single register
>or memory location could hold 6 characters.

A single machine word was 60 bits, so a single register read got you 10
characters.  There were three sets of registers -- the X registers were 60
bits, the A and B registers were 18 bits, which was the size of the largest
possible address.

CDC didn't actually use the term "byte".  That was IBM's domain.

When ASCII became unavoidable, most programs changed to using 5x 12-bit
"bytes" per word.

Ah, memories.  I spent 10 years working for Control Data.
-- 
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-04 Thread Chris Angelico
On Wed, Jun 5, 2013 at 5:51 AM, Joshua Landau
 wrote:
> On 4 June 2013 14:39, Grant Edwards  wrote:
>> On 2013-06-03, Dan Stromberg  wrote:
>>> Today though, it would be difficult to sell a conventional (Von Neumann)
>>> computer that didn't have 8 bit bytes.
>>
>> There are tons (as in millions of units per month) of CPUs still being
>> sold in the DSP market with 16, 20, 24, and 32 bit "bytes".  (When
>> writing C on a TMS320Cxx CPU sizeof (char) == sizeof (int) == sizeof
>> (long) == sizeof (float) == sizeof (double) == 1.  They all contain 32
>> bits.
> )
>
> *) for the bracket not in the reply
>
> Sorry.

So... can we cite http://xkcd.com/859/ in two threads at once, or does
that create twice as much tension?

Once an XKCD is un-cited, will it be garbage collected promptly, or do
they contain refloops?

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-04 Thread Joshua Landau
On 4 June 2013 14:39, Grant Edwards  wrote:
> On 2013-06-03, Dan Stromberg  wrote:
>> Today though, it would be difficult to sell a conventional (Von Neumann)
>> computer that didn't have 8 bit bytes.
>
> There are tons (as in millions of units per month) of CPUs still being
> sold in the DSP market with 16, 20, 24, and 32 bit "bytes".  (When
> writing C on a TMS320Cxx CPU sizeof (char) == sizeof (int) == sizeof
> (long) == sizeof (float) == sizeof (double) == 1.  They all contain 32
> bits.
)

*) for the bracket not in the reply

Sorry.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-04 Thread Carlos Nepomuceno


> From: invalid@invalid.invalid
> Subject: Re: How to get an integer from a sequence of bytes
> Date: Tue, 4 Jun 2013 13:42:46 +
> To: python-list@python.org
[...]
> VN designs are still very common in smaller CPUs (embedded stuff).

DSPs perhaps... not CPUs. Even ARMs are Harvard variants.

> Even modern desktop CPUs are "logically" still Von Neumann designs
> from the programmer's point of view (there's only a single address
> space for both data and instructions).  The fact that there are two
> sparate caches is almost entirely hidden from the user.  If you start
> to do stuff like write self-modifying code, then _may_ start having to
> worry about cache coherency.

Code/data separation isn't the only aspect. VN architecture is totally serial, 
even for RAM.

It's been a while since we've got into the multi-core, multipath world. Even in 
embedded devices.

> -- 
> Grant Edwards   grant.b.edwardsYow! I request a weekend in
>   at   Havana with Phil Silvers!
>   gmail.com
> -- 
> http://mail.python.org/mailman/listinfo/python-list
  -- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-04 Thread Grant Edwards
On 2013-06-03, Carlos Nepomuceno  wrote:
> 
>> Date: Mon, 3 Jun 2013 15:41:41 -0700 
>> Subject: Re: How to get an integer from a sequence of bytes 
>> From: drsali...@gmail.com 
>> To: python-list@python.org 
> [...]
>> Today though, it would be difficult to sell a conventional (Von  
>> Neumann) computer that didn't have 8 bit bytes.  Quantum computers  
>> would still sell if they were odd this way - they're going to be really  
>> different anyway. 
>
> Nowadays it would be a hard task to find a Von Neumann architecture
> machine.
>
> Most of current CPUs are variants of the Harvard architecture: they
> separate instructions from data at the cache level.  

VN designs are still very common in smaller CPUs (embedded stuff).

Even modern desktop CPUs are "logically" still Von Neumann designs
from the programmer's point of view (there's only a single address
space for both data and instructions).  The fact that there are two
sparate caches is almost entirely hidden from the user.  If you start
to do stuff like write self-modifying code, then _may_ start having to
worry about cache coherency.

-- 
Grant Edwards   grant.b.edwardsYow! I request a weekend in
  at   Havana with Phil Silvers!
  gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-04 Thread Grant Edwards
On 2013-06-03, Dan Stromberg  wrote:
> On Mon, Jun 3, 2013 at 7:31 AM, Grant Edwards wrote:
>
>> That's a common assumption, but historically, a "byte" was merely the
>> smallest addressable unit of memory.  The size of a "byte" on widely
>> used used CPUs ranged from 4 bits to 60 bits.
>>
>> Quoting from http://en.wikipedia.org/wiki/Byte
>>
>> "The size of the byte has historically been hardware
>>  dependent and no definitive standards existed that mandated the
>>  size."
>>
>> That's why IEEE standards always use the word "octet" when referring a
>> value containing 8 bits.
>
> When I was a Freshman in college, I used a CDC Cyber a lot; it had 6 bit
> bytes and 60 bit words.  This was in 1985.

But you couldn't address individual 6-bit "hextets" in memory could
you?  My recollection is that incrementing a memory address got you
the next 60-bit chunk -- that means that by the older terminology a
"byte" was 60 bits.  A "character" was 6 bits, and a single register
or memory location could hold 6 characters.

> Today though, it would be difficult to sell a conventional (Von Neumann)
> computer that didn't have 8 bit bytes.

There are tons (as in millions of units per month) of CPUs still being
sold in the DSP market with 16, 20, 24, and 32 bit "bytes".  (When
writing C on a TMS320Cxx CPU sizeof (char) == sizeof (int) == sizeof
(long) == sizeof (float) == sizeof (double) == 1.  They all contain 32
bits.

> Quantum computers would still sell if they were odd this way -
> they're going to be really different anyway.

-- 
Grant Edwards   grant.b.edwardsYow! Either CONFESS now or
  at   we go to "PEOPLE'S COURT"!!
  gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-03 Thread Dan Stromberg
On Mon, Jun 3, 2013 at 4:18 PM, Carlos Nepomuceno <
carlosnepomuc...@outlook.com> wrote:

> 
> > Date: Mon, 3 Jun 2013 15:41:41 -0700
> > Subject: Re: How to get an integer from a sequence of bytes
> > From: drsali...@gmail.com
> > To: python-list@python.org
> [...]
> > Today though, it would be difficult to sell a conventional (Von
> > Neumann) computer that didn't have 8 bit bytes.  Quantum computers
> > would still sell if they were odd this way - they're going to be really
> > different anyway.
>
> Nowadays it would be a hard task to find a Von Neumann architecture
> machine.
>
> Most of current CPUs are variants of the Harvard architecture: they
> separate instructions from data at the cache level.
>

I stand corrected.  Thank you.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-03 Thread Carlos Nepomuceno

> Date: Mon, 3 Jun 2013 15:41:41 -0700 
> Subject: Re: How to get an integer from a sequence of bytes 
> From: drsali...@gmail.com 
> To: python-list@python.org 
[...]
> Today though, it would be difficult to sell a conventional (Von  
> Neumann) computer that didn't have 8 bit bytes.  Quantum computers  
> would still sell if they were odd this way - they're going to be really  
> different anyway. 

Nowadays it would be a hard task to find a Von Neumann architecture machine.

Most of current CPUs are variants of the Harvard architecture: they separate 
instructions from data at the cache level.  
 
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-03 Thread Dan Stromberg
On Mon, Jun 3, 2013 at 7:31 AM, Grant Edwards wrote:

> That's a common assumption, but historically, a "byte" was merely the
> smallest addressable unit of memory.  The size of a "byte" on widely
> used used CPUs ranged from 4 bits to 60 bits.
>
> Quoting from http://en.wikipedia.org/wiki/Byte
>
> "The size of the byte has historically been hardware
>  dependent and no definitive standards existed that mandated the
>  size."
>
> That's why IEEE standards always use the word "octet" when referring a
> value containing 8 bits.
>

When I was a Freshman in college, I used a CDC Cyber a lot; it had 6 bit
bytes and 60 bit words.  This was in 1985.

Today though, it would be difficult to sell a conventional (Von Neumann)
computer that didn't have 8 bit bytes.  Quantum computers would still sell
if they were odd this way - they're going to be really different anyway.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-03 Thread Grant Edwards
On 2013-06-03, Dave Angel  wrote:
> On 06/03/2013 10:31 AM, Grant Edwards wrote:
>> On 2013-06-03, Dennis Lee Bieber  wrote:
>>> On Sun, 02 Jun 2013 21:25:45 +0200, Mok-Kong Shen
>>>  declaimed the following in
>>> gmane.comp.python.general:
>>>
>>>
 b'7' is the byte with the character 7 in a certain code, so that's
 ok. In other PLs one assigns an int to a byte, with that int in either
>>>
>>> In other languages "byte" is an 8-bit signed/unsigned numeric.
>>
>> That's a common assumption, but historically, a "byte" was merely the
>> smallest addressable unit of memory.  The size of a "byte" on widely
>> used used CPUs ranged from 4 bits to 60 bits.
>>
>
> I recall rewriting the unpacking algorithm to get the 10 
> characters from each byte, on such a machine.

Yep.  IIRC there were CDC machines (Cyber 6600?) with a 60-bit wide
"byte" and a 6-bit wide upper-case-only character set.  ISTM that the
Pascal compiler limited you to 6 significant characters in variable
names so that it could use a simple single register compare while
doing symbol lookups...

I think some IBM machines had 60-bit "bytes" as well.

-- 
Grant Edwards   grant.b.edwardsYow! DIDI ... is that a
  at   MARTIAN name, or, are we
  gmail.comin ISRAEL?
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-03 Thread Dave Angel

On 06/03/2013 10:31 AM, Grant Edwards wrote:

On 2013-06-03, Dennis Lee Bieber  wrote:

On Sun, 02 Jun 2013 21:25:45 +0200, Mok-Kong Shen
 declaimed the following in
gmane.comp.python.general:



b'7' is the byte with the character 7 in a certain code, so that's
ok. In other PLs one assigns an int to a byte, with that int in either


In other languages "byte" is an 8-bit signed/unsigned numeric.


That's a common assumption, but historically, a "byte" was merely the
smallest addressable unit of memory.  The size of a "byte" on widely
used used CPUs ranged from 4 bits to 60 bits.



 I recall rewriting the unpacking algorithm to get the 10 
characters from each byte, on such a machine.



--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-03 Thread Grant Edwards
On 2013-06-03, Dennis Lee Bieber  wrote:
> On Sun, 02 Jun 2013 21:25:45 +0200, Mok-Kong Shen
> declaimed the following in
> gmane.comp.python.general:
>
>
>> b'7' is the byte with the character 7 in a certain code, so that's
>> ok. In other PLs one assigns an int to a byte, with that int in either
>
>   In other languages "byte" is an 8-bit signed/unsigned numeric.

That's a common assumption, but historically, a "byte" was merely the
smallest addressable unit of memory.  The size of a "byte" on widely
used used CPUs ranged from 4 bits to 60 bits.

Quoting from http://en.wikipedia.org/wiki/Byte

"The size of the byte has historically been hardware
 dependent and no definitive standards existed that mandated the
 size."

That's why IEEE standards always use the word "octet" when referring a
value containing 8 bits.
 
Only recently has it become common to assume that an "byte" contains 8
bits.

-- 
Grant Edwards   grant.b.edwardsYow! It's a lot of fun
  at   being alive ... I wonder if
  gmail.commy bed is made?!?
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-02 Thread Chris Angelico
On Mon, Jun 3, 2013 at 5:25 AM, Mok-Kong Shen  wrote:
> b'7' is the byte with the character 7 in a certain code, so that's
> ok. In other PLs one assigns an int to a byte, with that int in either
> decimal notation or hexadecimal notation, or else one assigns a
> character to it, in which case it gets the value of the character
> in a certain code. What I don't yet understand is why Python is
> apprently different from other PLs in that point in not allowing direct
> coersion of a byte to an int.

It does. Just subscript it:

>>> b'7'[0]
55

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-06-02 Thread Mok-Kong Shen

Am 30.05.2013 21:22, schrieb Ned Batchelder:


On 5/30/2013 2:26 PM, Mok-Kong Shen wrote:

Am 27.05.2013 17:30, schrieb Ned Batchelder:

On 5/27/2013 10:45 AM, 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?


The next thing in the docs after int.to_bytes is int.from_bytes:
http://docs.python.org/3.3/library/stdtypes.html#int.from_bytes


I am sorry to have overlooked that. But one thing I yet wonder is why
there is no direct possibilty of converting a byte to an int in [0,255],
i.e. with a constrct int(b), where b is a byte.



Presumably you want this to work:

 >>> int(b'\x03')
 3

But you also want this to work:

 >>> int(b'7')
 7

These two interpretations are incompatible.  If b'\x03' becomes 3, then
shouldn't b'\x37' become 55?  But b'\x37' is b'7', and you want that to
be 7.


b'7' is the byte with the character 7 in a certain code, so that's
ok. In other PLs one assigns an int to a byte, with that int in either
decimal notation or hexadecimal notation, or else one assigns a
character to it, in which case it gets the value of the character
in a certain code. What I don't yet understand is why Python is
apprently different from other PLs in that point in not allowing direct
coersion of a byte to an int.

M. K. Shen


--
http://mail.python.org/mailman/listinfo/python-list


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

2013-05-30 Thread Ned Batchelder


On 5/30/2013 2:26 PM, Mok-Kong Shen wrote:

Am 27.05.2013 17:30, schrieb Ned Batchelder:

On 5/27/2013 10:45 AM, 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?


The next thing in the docs after int.to_bytes is int.from_bytes:
http://docs.python.org/3.3/library/stdtypes.html#int.from_bytes


I am sorry to have overlooked that. But one thing I yet wonder is why
there is no direct possibilty of converting a byte to an int in [0,255],
i.e. with a constrct int(b), where b is a byte.



Presumably you want this to work:

>>> int(b'\x03')
3

But you also want this to work:

>>> int(b'7')
7

These two interpretations are incompatible.  If b'\x03' becomes 3, then 
shouldn't b'\x37' become 55?  But b'\x37' is b'7', and you want that to 
be 7.


--Ned.


M. K. Shen



--
http://mail.python.org/mailman/listinfo/python-list


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

2013-05-30 Thread jmfauth
On 30 mai, 20:42, Ian Kelly  wrote:
> On Thu, May 30, 2013 at 12:26 PM, Mok-Kong Shen
>
>  wrote:
> > Am 27.05.2013 17:30, schrieb Ned Batchelder:
>
> >> On 5/27/2013 10:45 AM, 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?
>
> >> The next thing in the docs after int.to_bytes is int.from_bytes:
> >>http://docs.python.org/3.3/library/stdtypes.html#int.from_bytes
>
> > I am sorry to have overlooked that. But one thing I yet wonder is why
> > there is no direct possibilty of converting a byte to an int in [0,255],
> > i.e. with a constrct int(b), where b is a byte.
>
> The bytes object can be viewed as a sequence of ints.  So if b is a
> bytes object of non-zero length, then b[0] is an int in range(0, 256).



Well, Python now "speaks" only "integer", the rest is
commodity and there is a good coherency.

>>> bin(255)
'0b'
>>> oct(255)
'0o377'
>>> 255
255
>>> hex(255)
'0xff'
>>>
>>> int('0b', 2)
255
>>> int('0o377', 8)
255
>>> int('255')
255
>>> int('0xff', 16)
255
>>>
>>> 0b
255
>>> 0o377
255
>>> 255
255
>>> 0xff
255
>>>
>>> type(0b)

>>> type(0o377)

>>> type(255)

>>> type(0xff)


jmf
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-05-30 Thread Ian Kelly
On Thu, May 30, 2013 at 12:26 PM, Mok-Kong Shen
 wrote:
> Am 27.05.2013 17:30, schrieb Ned Batchelder:
>>
>> On 5/27/2013 10:45 AM, 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?
>>>
>> The next thing in the docs after int.to_bytes is int.from_bytes:
>> http://docs.python.org/3.3/library/stdtypes.html#int.from_bytes
>
>
> I am sorry to have overlooked that. But one thing I yet wonder is why
> there is no direct possibilty of converting a byte to an int in [0,255],
> i.e. with a constrct int(b), where b is a byte.

The bytes object can be viewed as a sequence of ints.  So if b is a
bytes object of non-zero length, then b[0] is an int in range(0, 256).
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-05-30 Thread Mok-Kong Shen

Am 27.05.2013 17:30, schrieb Ned Batchelder:

On 5/27/2013 10:45 AM, 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?


The next thing in the docs after int.to_bytes is int.from_bytes:
http://docs.python.org/3.3/library/stdtypes.html#int.from_bytes


I am sorry to have overlooked that. But one thing I yet wonder is why
there is no direct possibilty of converting a byte to an int in [0,255],
i.e. with a constrct int(b), where b is a byte.

M. K. Shen

--
http://mail.python.org/mailman/listinfo/python-list


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

2013-05-28 Thread Grant Edwards
On 2013-05-27, 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?

One way is using the struct module.

-- 
Grant Edwards   grant.b.edwardsYow! Uh-oh!!  I forgot
  at   to submit to COMPULSORY
  gmail.comURINALYSIS!
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-05-27 Thread Dave Angel

On 05/27/2013 08:31 PM, Steven D'Aprano wrote:

On Mon, 27 May 2013 11:30:18 -0400, Ned Batchelder wrote:


On 5/27/2013 10:45 AM, 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?



The next thing in the docs after int.to_bytes is int.from_bytes:


And I can't believe I missed that too :-(



And that approach probably works for negative ints too.


--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list


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

2013-05-27 Thread Carlos Nepomuceno

> From: steve+comp.lang.pyt...@pearwood.info
> Subject: Re: How to get an integer from a sequence of bytes
> Date: Mon, 27 May 2013 15:00:39 +
> 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 & 0b))
    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 & 0b))
    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? 
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-05-27 Thread Steven D'Aprano
On Mon, 27 May 2013 11:30:18 -0400, Ned Batchelder wrote:

> On 5/27/2013 10:45 AM, 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?
>>
>>
> The next thing in the docs after int.to_bytes is int.from_bytes:

And I can't believe I missed that too :-(


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2013-05-27 Thread Ned Batchelder

On 5/27/2013 10:45 AM, 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?



The next thing in the docs after int.to_bytes is int.from_bytes: 
http://docs.python.org/3.3/library/stdtypes.html#int.from_bytes


--Ned.


Thanks in advance.

M. K. Shen


--
http://mail.python.org/mailman/listinfo/python-list


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

2013-05-27 Thread Steven D'Aprano
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