Question about binary file reading

Hendrik van Rooyen mail at microcorp.co.za
Thu Mar 5 08:37:12 CET 2009


"Benjamin Peterson" <be.. at phon.org> wrote:


>So called encodings like "hex" and "rot13" are abuse of
>encode() method. encode() should translate
>between byte strings and unicode, not preform
>transformations like that. This has been removed
>in 3.x, so you should use binascii.

When all else fails, and just for fun, go to first principles:

>>> hextab = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']
>>> s = 'the quick brown fox jums OVER the lazy dog'
>>> h = []
>>> for x in s:
           h.append(hextab[ord(x)>>4])
           h.append(hextab[ord(x)&15])

>>> print ''.join(h)
74686520717569636B2062726F776E20666F78206A756D73204F56455220746865206C617A792064
6F67
>>>

- Hendrik











More information about the Python-list mailing list