On 06/08/2012 21:46, Mok-Kong Shen wrote:

If I have a string "abcd" then, with 8-bit encoding of each character,
there is a corresponding 32-bit binary integer. How could I best
obtain that integer and from that integer backwards again obtain the
original string? Thanks in advance.

Try this (Python 3, in which strings are Unicode):
import struct
>>> # For a little-endian integer
struct.unpack("<I", "abcd".encode("latin-1"))[0]
1684234849
hex(_)
'0x64636261'

or this (Python 2, in which strings are bytestrings):
>>> import struct
>>> # For a little-endian integer
>>> struct.unpack("<I", "abcd")[0]
1684234849
>>> hex(_)
'0x64636261'

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to