Advertisement
rfmonk

array_byteswap.py

Jan 13th, 2014
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.58 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3.  
  4. import array
  5. import binascii
  6.  
  7.  
  8. def to_hex(a):
  9.     chars_per_item = a.itemsize * 2
  10.     hex_version = binascii.hexlify(a)
  11.     num_chunks = len(hex_version) / chars_per_item
  12.     for i in xrange(num_chunks):
  13.         start = i * chars_per_item
  14.         end = start + chars_per_item
  15.         yield hex_version[start:end]
  16.  
  17. a1 = array.array('i', xrange(5))
  18. a2 = array.array('i', xrange(5))
  19. a2.byteswap()
  20.  
  21. fmt = '%10s %10s %10s %10s'
  22. print fmt % ('A1 hex', 'A1', 'A2 hex', 'A2')
  23. print fmt % (('-' * 10,) * 4)
  24. for values in zip(to_hex(a1), a1, to_hex(a2), a2):
  25.     print fmt % values
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement