Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def convert(numb):
- # getting HEX represent, in string, to use it when writing into binary file
- hexNumb = '{0:x}'.format(numb)
- out = ""
- # analyze input number, and compute
- if not capacity == 0:
- # checking power of two
- if log(capacity, 2) % 1 == 0.0 or True:
- # we have valid capacity of our integer.
- # now check the max number to given capacity, else raise an Exception
- if numb > maxInt:
- raise ValueError("Integer %s not filling in %s bytes capacity, Max INT: %s" %
- (numb, capacity, maxInt))
- # starting pushing to output
- for i in _grouper(2, hexNumb.zfill(capacity), '0'):
- # performing modification of izip_longest to string
- i = string.join(i, "")
- # print i
- out += chr(int(i, 16))
- # using statement below when i want a generator
- # yield chr(int(i, 16))
- else:
- raise ValueError("Capacity number must be a log of 2")
- return out
- # return normalized data from convert function
- def unconvert(numb):
- # using this hack to avoid bug with zero
- h = hexlify(numb)
- # print h
- if not "0"*cap == h:
- h = h.lstrip("0")
- return int(h, 16)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement