forked from marxin/script-misc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpretty-number.py
executable file
·37 lines (28 loc) · 923 Bytes
/
pretty-number.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python3
import sys
from textwrap import wrap
def pad_string(s, n, prefix=''):
if len(s) % n:
s = '0' * (n - len(s) % n) + s
return ' '.join(['%8s' % (prefix + x) for x in wrap(s, n)])
number = int(eval(sys.argv[1]))
print('%-16s: %d' % ('Integer', number))
hexadecimal = hex(abs(number))
assert hexadecimal.startswith('0x')
hexadecimal = hexadecimal[2:]
print('%-16s: %s' % ('Hexadecimal', pad_string(hexadecimal, 2, '0x')))
if len(hexadecimal) % 2:
hexadecimal = '0' + hexadecimal
print('%-16s: ' % 'Integer by byte', end='')
for b in wrap(hexadecimal, 2):
print('%8s ' % str(int(b, 16)), end='')
print()
binary = bin(abs(number))
assert binary.startswith('0b')
binary = binary[2:]
print('%-16s: %s' % ('Binary', pad_string(binary, 8)))
print('%-16s: ' % 'Bits set', end='')
for i, v in enumerate(reversed(binary)):
if v == '1':
print('%d ' % i, end='')
print()