[code.view]

[top] / python / PyMOTW / struct / struct_endianness.py

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2008 Doug Hellmann All rights reserved.
     #
     """
     """
     
     __version__ = "$Id$"
     #end_pymotw_header
     
     import struct
     import binascii
     
     values = (1, 'ab', 2.7)
     print 'Original values:', values
     
     endianness = [
         ('@', 'native, native'),
         ('=', 'native, standard'),
         ('<', 'little-endian'),
         ('>', 'big-endian'),
         ('!', 'network'),
         ]
     
     for code, name in endianness:
         s = struct.Struct(code + ' I 2s f')
         packed_data = s.pack(*values)
         print
         print 'Format string  :', s.format, 'for', name
         print 'Uses           :', s.size, 'bytes'
         print 'Packed Value   :', binascii.hexlify(packed_data)
         print 'Unpacked Value :', s.unpack(packed_data)

[top] / python / PyMOTW / struct / struct_endianness.py

contact | logmethods.com