I will say it first. It took 1 second. But it can be done in about 4 seconds.
Easy to use!
radix_conversion (number you want to convert, what base the number is, what base you want to convert)
Just enter!
For example
radix_conversion(0b11111111,2,16)
When you run
0xff
Is output.
You have successfully converted the binary number 0b11111111
to the hexadecimal number 0xff
.
It's so easy lol
Reference: Convert binary, octal, and hexadecimal numbers and strings to each other with Python
radix_conversion.py
def radix_conversion(value,a,b):
#10→~
if a == 10:
if b == 2:
num = print(bin(value))
if b == 8:
num = print(oct(value))
if b == 16:
num = print(hex(value))
#2→~
if a == 2:
a_string = str(value)
if b == 10:
num = print(int(a_string,0))
if b == 8:
num = print(format(int(a_string,0),'#o'))
if b == 16:
num = print(format(int(a_string,0),'#x'))
#8→~
if a == 8:
a_string = str(value)
if b == 10:
num = print(int(a_string,0))
if b == 2:
num = print(format(int(a_string,0),'#b'))
if b == 16:
num = print(format(int(a_string,0),'#x'))
#16→~
if a == 16:
a_string = str(value)
if b == 10:
num = print(int(a_string,0))
if b == 2:
num = print(format(int(a_string,0),'#b'))
if b == 8:
num = print(format(int(a_string,0),'#o'))
return num
Recommended Posts