Advertisement
rfmonk

codecs_to_hex.py

Jan 28th, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.52 KB | None | 0 0
  1. #1/usr/bin/env python
  2.  
  3. # this is from The Python
  4. # Standard Library by example
  5. # ISBN13: 9780321767349
  6.  
  7. import binascii
  8.  
  9.  
  10. def to_hex(t, nbytes):
  11.     """Format text t as a sequence of nbyte long values
  12.    seperated by spaces.
  13.    """
  14.  
  15.     chars_per_item = nbytes * 2
  16.     hex_version = binascii.hexlify(t)
  17.     return ' '.join(
  18.         hex_version[start:start + chars_per_item]
  19.         for start in xrange(0, len(hex_version), chars_per_item)
  20.     )
  21.  
  22. if __name__ == '__main__':
  23.     print to_hex('abcdef', 1)
  24.     print to_hex('abcdef', 2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement