Tuesday, May 5, 2009

Python: parsing big integers

Write a one line Python function to parse a integer from it's representation, e.g., '\x01\x02\x03\x04\x05' should result in an integer representing 0x0102030405.

1 comment:

Kundan said...

The trick is to use built-in long/int in Python to typecast from a string representation of the number in Hex. Writing Hex representation from binary representation is straight forward.

bin2int = lambda x: long(''.join('%02x'%(ord(a)) for a in x), 16)