Advertisement
Bkmz

Untitled

Oct 6th, 2011
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.37 KB | None | 0 0
  1. def convert(numb):
  2.     # getting HEX represent, in string, to use it when writing into binary file
  3.     hexNumb = '{0:x}'.format(numb)
  4.     out = ""
  5.    
  6.     # analyze input number, and compute
  7.     if not capacity == 0:
  8.         # checking power of two
  9.         if log(capacity, 2) % 1 == 0.0 or True:
  10.             # we have valid capacity of our integer.
  11.             # now check the max number to given capacity, else raise an Exception
  12.             if numb > maxInt:
  13.                 raise ValueError("Integer %s not filling in %s bytes capacity, Max INT: %s" %
  14.                                                             (numb, capacity, maxInt))
  15.            
  16.             # starting pushing to output
  17.             for i in _grouper(2, hexNumb.zfill(capacity), '0'):
  18.                 # performing modification of izip_longest to string
  19.                 i = string.join(i, "")
  20. #                print i
  21.                 out += chr(int(i, 16))
  22.                 # using statement below when i want a generator
  23.                 # yield chr(int(i, 16))
  24.         else:
  25.             raise ValueError("Capacity number must be a log of 2")
  26.        
  27.     return out
  28.  
  29. # return normalized data from convert function
  30. def unconvert(numb):
  31.     # using this hack to avoid bug with zero
  32.     h = hexlify(numb)
  33. #    print h
  34.     if not "0"*cap == h:
  35.         h = h.lstrip("0")
  36.        
  37.     return int(h, 16)
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement