Need elegant way to cast four bytes into a long

Grant Edwards grante at visi.com
Fri Aug 8 12:01:13 EDT 2003


In article <3f33c785$0$172$a1866201 at newsreader.visi.com>, Grant Edwards wrote:
> In article <3f33c209$1 at news.si.com>, William S. Huizinga wrote:
>> I've got an array.array of unsigned char and would like to make a slice
>> of that array (e.g. a[0:4]) become one long like I would in "C" :
>> 
>> 	l = ((unsigned long *) (&a[0]))[0];
> 
> Bad C programming.  Your program isn't portable.

Aside from the endian issue, it may even cause a bus fault or
completely bogus value on many architectures (ARM, SPARC,
etc.).  It's actually quite difficult to duplicate that sort of
behavior in Python.  ;)

-- 
Grant Edwards                   grante             Yow!  Remember, in 2039,
                                  at               MOUSSE & PASTA will
                               visi.com            be available ONLY by
                                                   prescription!!




More information about the Python-list mailing list