Need help with an array problem.

jakobsg at gmail.com jakobsg at gmail.com
Mon Oct 2 18:38:58 EDT 2006


To your question on casting long to short. This is how:
a=1234L     # long
b=int(a)       # int (short)

John Machin skrev:
> SpreadTooThin wrote:
> > Basically I think the problem is in converting from a 32 bit integer to
> > a 16 bit integer.
> >
> > I have two arrays:
> > import array
> >
> > a = array.array('L', [65537])
> > b = array.array('H', [0])
> >
> > b[0] = a[0]
> >
> > Which gives an overflow message....
>
> As it should.
>
> > So can't I truncate the long by discaring the upper bits ..
> > Like  b[0] = 0x0000FFFF & a[0]
>
> Any reason why you don't you just try it?
>
> | >>> import array
> | >>> a = array.array('L', [65537])
> | >>> b = array.array('H', [0])
> | >>> a[0]
> | 65537L
> | >>> a[0] & 0xffff
> | 1L
> | >>> b[0] = a[0] & 0xffff
> | >>> b[0]
> | 1
> | >>>




More information about the Python-list mailing list