2020
Number System Converter
Related Links
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
pythonorpython3(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: inverts0to1and1to0in 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))
