root/hodgestar/PythonCode/SmallScripts/int2bin.py

Revision 481, 0.8 kB (checked in by hodgestar, 3 years ago)

Use list rather than generator comprehension.

Line 
1# Cocooncrash's Version
2
3def cocoon_bin(i):
4    """Convert an integer to a string containing its binary representation."""
5    return "".join(map(lambda y:str((i>>y)&1), range(8-1, -1, -1)))
6
7
8# Mithrandi's Version
9
10DIGITS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
11
12def format_base(n, base):
13    digits = []
14    while n:
15        n, d = divmod(n, base)
16        digits.append(DIGITS[d])
17    return ''.join(reversed(digits))
18
19def mith_bin(i):
20    """Convert an integer to a string containing its binary representation."""
21    return format_base(i, 16)
22
23
24# Hodgestar's Version
25
26hex2bin = dict([ (hex(i)[2:], cocoon_bin(i)[4:]) for i in range(16) ])
27
28def bin(i):
29    """Convert an integer to a string containing its binary representation."""
30    return "".join([hex2bin[s] for s in hex(i)[2:]])
Note: See TracBrowser for help on using the browser.