Skip to content

Binary to Decimal Converter

Abstract

Binary to Decimal Converter is a Python application that performs bidirectional conversion between binary and decimal number systems. The program provides a menu-driven interface allowing users to convert binary numbers to decimal or decimal numbers to binary. This project demonstrates fundamental concepts of number systems, base conversions, user input handling, and menu-based program design. It’s an excellent project for understanding how computers represent numbers internally.

Prerequisites

  • Python 3.6 or above
  • A code editor or IDE
  • Basic understanding of number systems

Before you Start

Before starting this project, you must have Python installed on your computer. If you don’t have Python installed, you can download it from here. You must have a code editor or IDE installed on your computer. If you don’t have any code editor or IDE installed, you can download Visual Studio Code from here.

Note: This project uses only built-in Python features, so no additional installations are required.

Getting Started

Create a Project

  1. Create a folder named binary-decimal-converterbinary-decimal-converter.
  2. Open the folder in your favorite code editor or IDE.
  3. Create a file named binary_to_decimal.pybinary_to_decimal.py.
  4. Copy the given code and paste it in your binary_to_decimal.pybinary_to_decimal.py file.

Write the Code

  1. Copy and paste the following code in your binary_to_decimal.pybinary_to_decimal.py file.
⚙️ Binary to Decimal Converter
Binary to Decimal Converter
# Binary to Decimal Conveter
 
print("Binary to Decimal Conveter")
print("Choose one of the following options:")
print("1. Convert a binary number to a decimal number")
print("2. Convert a decimal number to a binary number")
print("3. Exit")
 
option = int(input("Enter your option: "))
if option == 1:
    binary = input("Enter a binary number: ")
    decimal = int(binary, 2)
    print("The decimal value of", binary, "is", decimal)
elif option == 2:
    decimal = int(input("Enter a decimal number: "))
    binary = bin(decimal)
    print("The binary value of", decimal, "is", binary)
elif option == 3:
    exit() 
Binary to Decimal Converter
# Binary to Decimal Conveter
 
print("Binary to Decimal Conveter")
print("Choose one of the following options:")
print("1. Convert a binary number to a decimal number")
print("2. Convert a decimal number to a binary number")
print("3. Exit")
 
option = int(input("Enter your option: "))
if option == 1:
    binary = input("Enter a binary number: ")
    decimal = int(binary, 2)
    print("The decimal value of", binary, "is", decimal)
elif option == 2:
    decimal = int(input("Enter a decimal number: "))
    binary = bin(decimal)
    print("The binary value of", decimal, "is", binary)
elif option == 3:
    exit() 
  1. Save the file.
  2. Open the terminal in your code editor or IDE and navigate to the folder binary-decimal-converterbinary-decimal-converter.
command
C:\Users\Your Name\binary-decimal-converter> python binary_to_decimal.py
Binary to Decimal Conveter
Choose one of the following options:
1. Convert a binary number to a decimal number
2. Convert a decimal number to a binary number
3. Exit
Enter your option: 1
Enter a binary number: 1101
The decimal value of 1101 is 13
 
C:\Users\Your Name\binary-decimal-converter> python binary_to_decimal.py
Binary to Decimal Conveter
Choose one of the following options:
1. Convert a binary number to a decimal number
2. Convert a decimal number to a binary number
3. Exit
Enter your option: 2
Enter a decimal number: 13
The binary value of 13 is 0b1101
 
C:\Users\Your Name\binary-decimal-converter> python binary_to_decimal.py
Binary to Decimal Conveter
Choose one of the following options:
1. Convert a binary number to a decimal number
2. Convert a decimal number to a binary number
3. Exit
Enter your option: 3
command
C:\Users\Your Name\binary-decimal-converter> python binary_to_decimal.py
Binary to Decimal Conveter
Choose one of the following options:
1. Convert a binary number to a decimal number
2. Convert a decimal number to a binary number
3. Exit
Enter your option: 1
Enter a binary number: 1101
The decimal value of 1101 is 13
 
C:\Users\Your Name\binary-decimal-converter> python binary_to_decimal.py
Binary to Decimal Conveter
Choose one of the following options:
1. Convert a binary number to a decimal number
2. Convert a decimal number to a binary number
3. Exit
Enter your option: 2
Enter a decimal number: 13
The binary value of 13 is 0b1101
 
C:\Users\Your Name\binary-decimal-converter> python binary_to_decimal.py
Binary to Decimal Conveter
Choose one of the following options:
1. Convert a binary number to a decimal number
2. Convert a decimal number to a binary number
3. Exit
Enter your option: 3

Explanation

Understanding Number Systems

  • Binary (Base 2): Uses only digits 0 and 1
  • Decimal (Base 10): Uses digits 0-9 (our standard counting system)
  • Conversion: Process of changing from one base to another

Code Breakdown

  1. Display the program title and menu options.
binary_to_decimal.py
print("Binary to Decimal Conveter")
print("Choose one of the following options:")
print("1. Convert a binary number to a decimal number")
print("2. Convert a decimal number to a binary number")
print("3. Exit")
binary_to_decimal.py
print("Binary to Decimal Conveter")
print("Choose one of the following options:")
print("1. Convert a binary number to a decimal number")
print("2. Convert a decimal number to a binary number")
print("3. Exit")
  1. Get user’s choice.
binary_to_decimal.py
option = int(input("Enter your option: "))
binary_to_decimal.py
option = int(input("Enter your option: "))
  1. Handle binary to decimal conversion.
binary_to_decimal.py
if option == 1:
    binary = input("Enter a binary number: ")
    decimal = int(binary, 2)
    print("The decimal value of", binary, "is", decimal)
binary_to_decimal.py
if option == 1:
    binary = input("Enter a binary number: ")
    decimal = int(binary, 2)
    print("The decimal value of", binary, "is", decimal)
  1. Handle decimal to binary conversion.
binary_to_decimal.py
elif option == 2:
    decimal = int(input("Enter a decimal number: "))
    binary = bin(decimal)
    print("The binary value of", decimal, "is", binary)
binary_to_decimal.py
elif option == 2:
    decimal = int(input("Enter a decimal number: "))
    binary = bin(decimal)
    print("The binary value of", decimal, "is", binary)
  1. Handle exit option.
binary_to_decimal.py
elif option == 3:
    exit()
binary_to_decimal.py
elif option == 3:
    exit()

Conversion Methods

Binary to Decimal

Python’s int()int() function with base parameter:

binary_to_decimal.py
decimal = int(binary_string, 2)
binary_to_decimal.py
decimal = int(binary_string, 2)

Manual calculation for 1101:

  • 1×2³ + 1×2² + 0×2¹ + 1×2⁰
  • 1×8 + 1×4 + 0×2 + 1×1
  • 8 + 4 + 0 + 1 = 13

Decimal to Binary

Python’s bin()bin() function:

binary_to_decimal.py
binary = bin(decimal_number)
binary_to_decimal.py
binary = bin(decimal_number)

Manual calculation for 13:

  • 13 ÷ 2 = 6 remainder 1
  • 6 ÷ 2 = 3 remainder 0
  • 3 ÷ 2 = 1 remainder 1
  • 1 ÷ 2 = 0 remainder 1
  • Reading remainders from bottom to top: 1101

Features

  • Bidirectional Conversion: Binary ↔ Decimal
  • Menu-Driven Interface: Easy-to-use option selection
  • Built-in Functions: Uses Python’s efficient conversion methods
  • Simple Input/Output: Clear prompts and results
  • Exit Option: Clean program termination
  • Error Handling: Basic input validation

Number System Examples

Binary to Decimal Examples

BinaryCalculationDecimal
10101×8 + 0×4 + 1×2 + 0×110
11111×8 + 1×4 + 1×2 + 1×115
100001×16 + 0×8 + 0×4 + 0×2 + 0×116

Decimal to Binary Examples

DecimalBinaryExplanation
51014 + 1
810008
25511111111128+64+32+16+8+4+2+1

Applications in Computer Science

  • Computer Architecture: How computers store numbers
  • Digital Logic: Foundation of digital circuits
  • Programming: Understanding data representation
  • Networking: IP addresses and subnetting
  • Cryptography: Binary operations in encryption

Next Steps

You can enhance this project by:

  • Adding input validation for binary numbers
  • Supporting other number systems (octal, hexadecimal)
  • Creating a GUI version using Tkinter
  • Adding batch conversion from files
  • Implementing custom conversion algorithms
  • Adding floating-point number support
  • Creating visualization of conversion process
  • Adding history of conversions
  • Implementing error handling for invalid inputs
  • Adding unit tests for validation

Enhanced Version Ideas

binary_to_decimal.py
def enhanced_converter():
    # Features to add:
    # - Support for octal and hexadecimal
    # - Input validation
    # - Conversion history
    # - Batch processing
    # - Custom base conversions
    pass
 
def validate_binary(binary_str):
    # Check if string contains only 0s and 1s
    return all(digit in '01' for digit in binary_str)
 
def manual_binary_to_decimal(binary_str):
    # Implement manual conversion algorithm
    decimal = 0
    for i, digit in enumerate(reversed(binary_str)):
        decimal += int(digit) * (2 ** i)
    return decimal
binary_to_decimal.py
def enhanced_converter():
    # Features to add:
    # - Support for octal and hexadecimal
    # - Input validation
    # - Conversion history
    # - Batch processing
    # - Custom base conversions
    pass
 
def validate_binary(binary_str):
    # Check if string contains only 0s and 1s
    return all(digit in '01' for digit in binary_str)
 
def manual_binary_to_decimal(binary_str):
    # Implement manual conversion algorithm
    decimal = 0
    for i, digit in enumerate(reversed(binary_str)):
        decimal += int(digit) * (2 ** i)
    return decimal

Manual Conversion Algorithms

Binary to Decimal (Manual)

binary_to_decimal.py
def binary_to_decimal_manual(binary):
    decimal = 0
    power = 0
    for digit in reversed(binary):
        decimal += int(digit) * (2 ** power)
        power += 1
    return decimal
binary_to_decimal.py
def binary_to_decimal_manual(binary):
    decimal = 0
    power = 0
    for digit in reversed(binary):
        decimal += int(digit) * (2 ** power)
        power += 1
    return decimal

Decimal to Binary (Manual)

binary_to_decimal.py
def decimal_to_binary_manual(decimal):
    if decimal == 0:
        return "0"
    binary = ""
    while decimal > 0:
        binary = str(decimal % 2) + binary
        decimal = decimal // 2
    return binary
binary_to_decimal.py
def decimal_to_binary_manual(decimal):
    if decimal == 0:
        return "0"
    binary = ""
    while decimal > 0:
        binary = str(decimal % 2) + binary
        decimal = decimal // 2
    return binary

Educational Value

This project teaches:

  • Number Systems: Understanding different bases
  • Built-in Functions: Using int() and bin()
  • Menu Design: Creating user-friendly interfaces
  • Conditional Logic: Handling different user choices
  • Input/Output: Processing user input and displaying results

Common Use Cases

  • Educational Tool: Teaching number system conversions
  • Programming Practice: Understanding data representation
  • Computer Science Fundamentals: Base conversion concepts
  • Digital Electronics: Understanding binary representation
  • Quick Calculations: Converting between systems

Error Handling Improvements

binary_to_decimal.py
def safe_binary_to_decimal():
    try:
        binary = input("Enter a binary number: ")
        if not all(digit in '01' for digit in binary):
            print("Error: Please enter only 0s and 1s")
            return
        decimal = int(binary, 2)
        print(f"The decimal value of {binary} is {decimal}")
    except ValueError:
        print("Error: Invalid binary number")
binary_to_decimal.py
def safe_binary_to_decimal():
    try:
        binary = input("Enter a binary number: ")
        if not all(digit in '01' for digit in binary):
            print("Error: Please enter only 0s and 1s")
            return
        decimal = int(binary, 2)
        print(f"The decimal value of {binary} is {decimal}")
    except ValueError:
        print("Error: Invalid binary number")

Performance Considerations

  • Built-in Functions: Very efficient for conversion
  • String Operations: Minimal overhead for typical inputs
  • Memory Usage: Constant space complexity
  • Large Numbers: Python handles arbitrary precision

Real-World Applications

  • Computer Programming: Understanding data types
  • Digital Systems: Binary logic and circuits
  • Network Administration: IP address calculations
  • Embedded Systems: Low-level programming
  • Data Analysis: Binary data processing

Conclusion

In this project, we learned how to create a Binary to Decimal Converter using Python’s built-in conversion functions. We explored fundamental concepts of number systems, menu-driven programming, and user interface design. This project provides essential knowledge for understanding how computers represent and process numerical data. The conversion between binary and decimal systems is fundamental to computer science and digital technology. To find more projects like this, you can visit Python Central Hub.

Was this page helpful?

Let us know how we did