[code.view]

[top] / python / PyMOTW / array / array_byteswap.py

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2008 Doug Hellmann All rights reserved.
     #
     """
     """
     
     __version__ = "$Id$"
     #end_pymotw_header
     
     import array
     import binascii
     
     def to_hex(a):
         chars_per_item = a.itemsize * 2 # 2 hex digits
         hex_version = binascii.hexlify(a)
         num_chunks = len(hex_version) / chars_per_item
         for i in xrange(num_chunks):
             start = i*chars_per_item
             end = start + chars_per_item
             yield hex_version[start:end]
     
     a1 = array.array('i', xrange(5))
     a2 = array.array('i', xrange(5))
     a2.byteswap()
     
     fmt = '%10s %10s %10s %10s'
     print fmt % ('A1 hex', 'A1', 'A2 hex', 'A2')
     print fmt % (('-' * 10,) * 4)
     for values in zip(to_hex(a1), a1, to_hex(a2), a2):
         print fmt % values

[top] / python / PyMOTW / array / array_byteswap.py

contact | logmethods.com