4-byte string to integer
Alan Gauld
alan.gauld at gssec.bt.co.uk
Wed Oct 4 13:08:15 EDT 2000
j vickroy wrote:
>
> How can a 4-byte string be converted to an unsigned integer using
> Python?
>
> The byte order may be either big or little endian.
Last things first, you will need to know that. There
is no way the program can tell unless you tell it.
Now assuming the bytes are integers making up the
digits you can just multiply and add succesive digits
by successive powers of 10.
Thus for 1,2,3,4:
digits = (1,2,3,4)
m,n = 1,0
for i in (1,2,3,4):
n = n + i*m
m = 10 * m
If the order is different you need to rearrange the
order accordingly first.
digits = (digits[1],digits[0], digits[3],digits[2])
You can of course generalise this but if its only
ever 4 digits its not worth it.
HTH,
Alan G.
--
=================================================
This post represents the views of the author
and does not necessarily accurately represent
the views of BT.
More information about the Python-list
mailing list