I tried to create a program to convert hexadecimal numbers to decimal numbers with python

I created a program to convert hexadecimal numbers to decimal numbers for learning python.

If you use the int function as shown below, you can convert it in one shot, so it is just learning.

main.py


print(int('3b',base=16))

terminal


59

The code actually created looks like the following.

main.py


base_num = '0123456789ABCDEF'
count_num = 3
def hex_to_int(hex_str):#Convert HEX string to number
    i = len(hex_str)
    value = 0
    digits = 0
    while i > 0:
        value += base_num.find(hex_str[i - 1]) * (len(base_num) ** digits)
        i -= 1
        digits += 1
    return value

if __name__ == "__main__":
    num_list = []
    while len(num_list) < count_num:
        input_num = input('Please enter a hexadecimal number:')
        input_num = input_num.upper()

        #HEX string check
        is_num_check = True
        for num in input_num:
            if not num in base_num:
                is_num_check = False

        if is_num_check:
            input_val = hex_to_int(input_num)
            num_list.append(input_val)
        else:
            print('Not a hexadecimal number')

    print(*num_list)

Converts the entered hexadecimal number value to decimal number and returns it.

If you use the inclusion notation, you can write it shorter.

Recommended Posts

I tried to create a program to convert hexadecimal numbers to decimal numbers with python
I tried to create a list of prime numbers with python
I tried to create Bulls and Cows with a shell program
[Outlook] I tried to automatically create a daily report email with Python
[Python] Convert decimal numbers to binary numbers, octal numbers, and hexadecimal numbers
I tried to create a table only with Django
I tried to draw a route map with Python
I tried to automatically generate a password with Python3
When I tried to create a virtual environment with Python, it didn't work
I tried to easily create a fully automatic attendance system with Selenium + Python
Convert decimal numbers to n-ary numbers [python]
I tried to automatically create a report with Markov chain
I wrote a program quickly to study DI with Python ①
Mayungo's Python Learning Episode 3: I tried to print numbers with print
I made a program to convert images into ASCII art with Python and OpenCV
[Python] I tried to automatically create a daily report of YWT with Outlook mail
I tried scraping food recall information with Python to create a pandas data frame
I tried to create a linebot (implementation)
I tried to create a linebot (preparation)
I tried a functional language with Python
[5th] I tried to make a certain authenticator-like tool with python
I tried to convert a Python file to EXE (Recursion error supported)
[2nd] I tried to make a certain authenticator-like tool with python
[3rd] I tried to make a certain authenticator-like tool with python
[Python] A memo that I tried to get started with asyncio
I tried to make a periodical process with Selenium and Python
I tried to make a 2channel post notification application with Python
I tried to make a todo application using bottle with python
[4th] I tried to make a certain authenticator-like tool with python
[1st] I tried to make a certain authenticator-like tool with python
Python: I tried to make a flat / flat_map just right with a generator
I tried to communicate with a remote server by Socket communication with Python.
I want to create a window in Python
Mayungo's Python Learning Episode 6: I tried to convert a character string to a number
I want to make a game with Python
[Python memo] I want to get a 2-digit hexadecimal number from a decimal number
I tried to create a plug-in with HULFT IoT Edge Streaming [Development] (2/3)
I tried to get CloudWatch data with Python
I tried to output LLVM IR with Python
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
I tried to create a plug-in with HULFT IoT Edge Streaming [Execution] (3/3)
I tried to discriminate a 6-digit number with a number discrimination application made with python
Steps to create a Twitter bot with python
I tried to automate sushi making with python
I tried to create a plug-in with HULFT IoT Edge Streaming [Setup] (1/3)
I tried to build a Mac Python development environment with pythonz + direnv
I tried to create a sample to access Salesforce using Python and Bottle
I want to write to a file with Python
I tried to make a periodical process with CentOS7, Selenium, Python and Chrome
I tried to make a simple mail sending application with tkinter of Python
I tried to create a class that can easily serialize Json in Python
Mayungo's Python Learning Episode 5: I tried to do four arithmetic operations with numbers
[Patent analysis] I tried to make a patent map with Python without spending money
[ES Lab] I tried to develop a WEB application with Python and Flask ②
I tried to create a button for Slack with Raspberry Pi + Tact Switch
I tried to create a model with the sample of Amazon SageMaker Autopilot
How to convert / restore a string with [] in python
I tried to implement Minesweeper on terminal with python
I tried to get started with blender python script_Part 01
Convert a text file with hexadecimal values to a binary file
I tried to touch the CSV file with Python