python and bit shifts and byte order, oh my!

Grant Edwards grante at visi.com
Fri Sep 10 16:10:37 EDT 2004


On 2004-09-10, Reid Nichol <rnichol_rrc at yahoo.com> wrote:

> It's my understanding that pack and unpack of the struct module returns 
> strings and not rearranged integers.

Partly right. Pack returns a string.  Unpack returns whatever
you tell it to return (integers, floats, etc.).

When reading a 4 byte integer from a file:

  Read a string of 4 bytes from the file.

  Use struct.upack() on the string to convert it to an integer
  object.

When writing a  4 byte integer to a file:

  Use struct.pack() to convert the integer object into a 4-byte
  string with the desired byte order.

  Write the 4 byte string to the file.  

> At any rate I would rather do it myself if only to teach
> myself something about this.

Do what yourself?

-- 
Grant Edwards                   grante             Yow!  NOW, I'm supposed
                                  at               to SCRAMBLE two, and HOLD
                               visi.com            th' MAYO!!



More information about the Python-list mailing list