Lab Number System Converter

2020

Number System Converter

Python
Number System Converter

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

  1. Download the script (nsc.py) to your computer.
  2. Open a command line window and type 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 or an advanced text editor. Some IDEs are IDLE, PyCharm, Visual Studio Code, Sublime Text and others.

Number systems used in the program

  • Decimal (base 10): uses digits 0–9.
  • Binary (base 2): uses digits 0 and 1.
  • Octal (base 8): uses digits 0–7.
  • Hexadecimal (base 16): uses digits 0–9 and characters A–F.

Functions used in the program

  • convert: transforms a number from one system to another.
  • remove: removes the extra printed characters "0b", "0o" or "0x".
  • invert: inverts 0 to 1 and 1 to 0 in string format.

How it works

The user gives the initial number and selects its system. The number is converted to an integer using one of:

int(num, 2)  # binary
int(num)     # decimal
int(num, 8)  # octal
int(num, 16) # hexadecimal

The result is stored in temp. The user then picks the target system:

temp      # decimal
bin(temp) # binary
oct(temp) # octal
hex(temp) # hexadecimal

Negative numbers

Example: converting -123 to hexadecimal.

# 1. Transform positive 123 to binary:
a = bin(123) # 1111011

# 2. Pad with 0's to 8 digits:
a = '0'*(8-len(a)) + a # 01111011

# 3. Invert every digit:
b = ''
for let in a:
    b += invert(let) # 10000100

# 4. Add 1 and clean the prefix:
c = remove(bin(int(b, 2) + int('1', 2)))

# 5. Convert to hexadecimal:
result = hex(int(c, 2)) # 85

# 6. Pad with F's to mark it negative:
# FFFFFF85

4th option: convert to all systems at once

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))

Frequently Asked Questions

Which number systems does the converter support?

Binary, decimal, octal and hexadecimal. It uses Python’s built-in int(), bin(), oct() and hex() functions.

How do I run the program?

Download nsc.py and run python3 path_to_script/nsc.py from a command line, or open it in an IDE or text editor such as IDLE, PyCharm, Visual Studio Code or Sublime Text.

How are negative numbers converted?

The positive value is converted to binary and padded to 8 digits, every digit is inverted, 1 is added, and the result is converted to the target system. For hexadecimal the result is padded with F’s, so -123 becomes FFFFFF85.

Can I convert a number to all systems at once?

Yes. Option 4 shows the result in decimal, octal and hexadecimal at the same time.

Badge