e_rsatz projects: ([info]e_rsatz) wrote,
@ 2005-08-19 16:33:00
Previous Entry  Add to memories!  Tell a Friend!  Next Entry
Entry tags:python

The Python I like - A string to integer converter
The following function converts a sequence of bytes into an integer. It is endianness-sensitive:

def ordn(big=1):

   if big % 2:

     def bordn(seq):
       if len(seq) > 1: return ord(seq[-1]) + (bordn(seq[:-1]) << 8)
       else: return ord(seq[0])
     return bordn

   else:

     def lordn(seq):
       if len(seq) >1: return ord(seq[0]) + (lordn(seq[1:]) << 8)
       else: return ord(seq[0])
     return lordn

>>> ordn(1)('\x01\x00')
256
>>> ordn(0)('\x01\x00')
1

Ok, that's probably not the prettiest nor the fastest way to do...
This entry



Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…