[Python] Created a method to convert radix in 1 second

I will say it first. It took 1 second. But it can be done in about 4 seconds.

How to use

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.

Details

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

[Python] Created a method to convert radix in 1 second
How to convert / restore a string with [] in python
Publish / upload a library created in Python to PyPI
Convert markdown to PDF in Python
How to use the __call__ method in a Python class
I tried "How to get a method decorated in Python"
[Python] How to convert a 2D list to a 1D list
Convert psd file to png in Python
How to get a stacktrace in python
[For beginners] How to register a library created in Python in PyPI
Convert from Markdown to HTML in Python
I created a password tool in Python.
Convert absolute URLs to relative URLs in Python
I wrote a code to convert quaternions to z-y-x Euler angles in Python
How to send a visualization image of data created in Python to Typetalk
Note: [Python3] Convert datetime to a string in any format you like
I made a script in python to convert .md files to Scrapbox format
Convert FBX files to ASCII <-> BINARY in Python
Try to calculate a statistical problem in Python
How to clear tuples in a list (Python)
To execute a Python enumerate function in JavaScript
How to embed a variable in a python string
I want to create a window in Python
How to create a JSON file in Python
A clever way to time processing in Python
Steps to develop a web application in Python
How to notify a Discord channel in Python
[Python] How to draw a histogram in Matplotlib
Convert exponential notation float to str in Python
Convert cubic mesh code to WKT in Python
Simplex method (simplex method) in Python
Private method in python
[Python] Created a class to play sin waves in the background with pyaudio
Parse a JSON string written to a file in Python
I want to embed a variable in a Python string
I want to easily implement a timeout in python
Try to make a Python module in C language
[python] Method to darken RGB value in hexadecimal notation
Convert NumPy array "ndarray" to lilt in Python [tolist ()]
Convert CIDR notation netmask to dotted decimal notation in Python
I want to write in Python! (2) Let's write a test
[Python] How to expand variables in a character string
Create a plugin to run Python Doctest in Vim (2)
Create a plugin to run Python Doctest in Vim (1)
A memorandum to run a python script in a bat file
I want to randomly sample a file in Python
I want to work with a robot in python.
Things to note when initializing a list in Python
Convert callback-style asynchronous API to async / await in Python
Convert / return class object to JSON format in Python
Introduction to Linear Algebra in Python: A = LU Decomposition
Convert Webpay Entity type to Dict type (recursively in Python)
How to execute a command using subprocess in Python
Created a Python library to write complex comprehensions and reduce in an easy-to-read manner
A mechanism to call a Ruby method from Python that can be done in 200 lines
Take a screenshot in Python
[python] Convert date to string
Create a function in Python
To flush stdout in Python
Convert numpy int64 to python int
Create a dictionary in Python