-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathnumrepl.py
More file actions
executable file
·29 lines (23 loc) · 901 Bytes
/
Copy pathnumrepl.py
File metadata and controls
executable file
·29 lines (23 loc) · 901 Bytes
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
#!/usr/bin/env python3
# A tiny script that prints numbers in binary and hex,
# and also as bit_cast<i32>(static_cast<u32>(number)).
# Based on https://bernsteinbear.com/blog/simple-python-repl/
import code
import readline
import struct
class Repl(code.InteractiveConsole):
def runsource(self, source, filename="<input>", symbol="single"):
try:
num = int(source, 0) # `, 0` infers base from 0x prefix
except Exception as e:
print(f'error: {e}')
return False
length = len(format(num, '#b'))
print(f'bin: {num: >#{length}b}')
print(f'dec: {num: >#{length}}')
print(f'hex: {num: >#{length}x}')
i32 = struct.unpack("i", struct.pack("I", num))[0]
print(f'i32: {i32: >#{length}}')
print(f'x32: {i32: >#{length}x}')
repl = Repl()
repl.interact(banner='integer conversion repl', exitmsg='')