Programming tricks related to ActionScript, Python, C/C++ and Java. See the comments for answers.
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.
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)
1 comment:
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)
Post a Comment