Need elegant way to cast four bytes into a long

Skip Montanaro skip at pobox.com
Fri Aug 8 11:54:38 EDT 2003


    wsh> I have been getting what I want in this sort of manner :

    wsh>        l  = 0L
    wsh>        l  = a[0]
    wsh>        l += a[1] <<  8
    wsh>        l += a[2] << 16
    wsh>        l += a[3] << 24

    wsh> but I think that's too wordy.  Is there a more intrinsic and
    wsh> elegant way to do this?

You mean like this:

    l = long(a[0] + a[1] << 8 + a[2] << 16 + a[3] << 24)

You can use struct.unpack as well, though I'd be hard-pressed to get the
details correct.  You'd be better off with "pydoc struct".

Skip






More information about the Python-list mailing list