converting an array of chars to a string

Jeff Epler jepler at unpythonic.net
Mon Jun 24 12:56:21 EDT 2002


Earlier, JB [mailto:jblazi at hotmail.com] wrote:
> > Thank you very much. In principle, this was exactly, for 
> > what I was looking. But most unforunately if I declare such 
> > an array a, then
> > 
> > a[0]=210
> > a[0] += 46
> > 
> > produces an error instead of the correct result 0. Now I am 
> > a bit desperate.
> 
On Mon, Jun 24, 2002 at 09:59:05AM -0600, Bjorn Pettersen wrote:
> Python helpfully gives you an OverflowError since that's technically
> what it is. Having the value magically truncated would surprise even
> more people. The simple solution is of course:
> 
>   def byteAdd(a, b):
>       return (a + b) % 256
> 
>   a[0] = byteAdd(a[0], 46)
> 
> hth,
> -- bjorn

It seems like a "modulo arithmetic" array type would be useful.  

Hypothetically, 
>>> x = array.array("mB", [210])
>>> x[0] += 47
>>> x[0]
1

Jeff`





More information about the Python-list mailing list