PROJECTS / NUMBER SYSTEM CONVERTER

2020

Number System Converter

Languages:

This python project is about converting a number from one system to another with the appropriate commands for each system (binary, decimal, octal, hexadecimal).

How to run the program

1. Using command line

Download the script (nsc.py) to your computer.
Open a command line window and type in the word python or python3 (based on your Python version) followed by the script path.

python3 ../path_to_script/nsc.py

2. Using an IDE or a Text Editor

Run the script using an IDE (Integrated Development Environment) or an advanced text editor.
Some IDEs are IDLE, PyCharm, Visual Studio Code, Sublime Text and others.

Number systems used in the program

Functions used in the program

How it works

At first user is asked to give the initial number and at the same time to select the system in which this number belongs. Then based on these information the number which user has entered is being converted to an integer by one of the following commands :

int(num, 2)  # If num is the number and system is binary
int(num)     # if num is the number and system is decimal
int(num, 8)  # If num is the number and system is octal
int(num, 16) # If num is the number and system is hexadecimal

Then set variable temp equal to one of the above. The user is asked to choose in which system he would like to convert the initial number except the initial system. Finally the appropriate commands are used to convert the number.

temp      # Gives the number to decimal system
bin(temp) # Gives the number to binary system
oct(temp) # Gives the number to octal system
hex(temp) # Gives the number to hexadecimal system

Negative numbers

In case that user enters a negative number then the program uses the following procedure.
Let's say for example that we want to convert -123 to hexadecimal.

# 1. Transform positive 123 to decimal : 
   a = bin(123) # 1111011
# 2. Add 0's to decimal a if it's digits are less than 8 : 
   a = '0'*(8-len(a)) + a # 01111011
# 3. Invert all digits using the invert() function and save it as a string b. Initialize b='' : 
   for let in a:
       b += invert(let) # 10000100
# 4. Add '1' to the above result and remove extra chars using remove() function : 
   c = remove(bin(int(b, 2) + int('1', 2)))
# 5. Convert to hexadecimal system : 
   result = hex(int(c, 2)) # 85
# 6. Add F's in front of the result to show that number is negative.
   # Number of F's should be as many as needed to fill the length of 8 digits. (FFFFFF85)
System converter for negative number command line

4th option in transformations

Using this option (4) user can transform his number not only to one of the other 3 systems but in all three together at once.

# Transformations from Binary to (Decimal or Octal or Hexadecimal or all)
if my_system == 1:
    if system_choice == 1:
        result = temp
    elif system_choice == 2:
        result = oct(temp)
    elif system_choice == 3:
        result = hex(temp)
    elif system_choice == 4 :
        result = "dec : {}, oct : {}, hex : {}".format(temp, oct(temp), hex(temp))
System converter with 4th option
System converter output