๐Ÿ”ข Number Systems Overview#

A number system is a structured way to represent and manipulate numbers using a set of symbols and rules. Each system has a base that determines how many unique digits it uses.


๐ŸŒ Types of Number Systems#

Number System

Base

Digits Used

Example

Common Uses

Decimal

10

0โ€“9

42

Everyday arithmetic, finance

Binary

2

0, 1

101010

Computers, digital circuits

Octal

8

0โ€“7

52

Unix file permissions, legacy code

Hexadecimal

16

0โ€“9, Aโ€“F

2A

Memory addressing, color codes


๐Ÿง  Key Differences#

  • Base Value:
    Determines how many digits are used before rolling over to the next place value.

  • Digit Set:
    Binary uses only 0 and 1, while hexadecimal includes letters Aโ€“F for values 10โ€“15.

  • Compactness:
    Binary numbers are longer; hexadecimal and octal offer more compact representations.

  • Conversion:
    All systems can be converted to and from decimal. For example:

    • Binary 101010 โ†’ Decimal 42

    • Decimal 42 โ†’ Hexadecimal 2A

    • Decimal 42 โ†’ Octal 52


๐Ÿ“˜ Summary Table#

Feature

Decimal

Binary

Octal

Hexadecimal

Base

10

2

8

16

Digits Used

0โ€“9

0, 1

0โ€“7

0โ€“9, Aโ€“F

Example

42

101010

52

2A

Compactness

Moderate

Least

More than binary

Most

Use Case

Daily life

Computers

Legacy systems

Programming


Understanding these systems is essential for fields like computer science, engineering, and cybersecurity, where data representation and manipulation are foundational.

๐Ÿ”„ Interactive Number System Converter#

This tool allows users to convert numbers between Decimal, Binary, Octal, and Hexadecimal systems using Python and ipywidgets.


๐Ÿง  What It Does#

  • Accepts a number input and its base (e.g., Decimal, Binary).

  • Converts it to a target base selected by the user.

  • Displays the result and a brief interpretation.


โš™๏ธ How It Works#

  1. Input Handling:
    The user enters a number and selects its base (e.g., Decimal).

  2. Conversion Logic:

    • The input is first converted to Decimal using int(num_str, base_from).

    • Then itโ€™s converted to the target base using:

      • bin() for Binary

      • oct() for Octal

      • hex() for Hexadecimal

  3. Output Display:
    The result is shown using Markdown formatting, along with a clear explanation of the conversion.


โœ… Example#

Input:

  • Number: 42

  • From Base: Decimal

  • To Base: Binary

Output:

import ipywidgets as widgets
from IPython.display import display, clear_output, Markdown

# ๐Ÿ”ข Supported Bases
bases = {
    "Decimal": 10,
    "Binary": 2,
    "Octal": 8,
    "Hexadecimal": 16
}

# ๐Ÿ“ฅ Input Widgets
input_number = widgets.Text(
    value='42',
    description='๐Ÿ”ข Input Number:',
    style={'description_width': 'initial'},
    layout=widgets.Layout(width='400px')
)

input_base = widgets.Dropdown(
    options=bases.keys(),
    value='Decimal',
    description='๐Ÿ“ฅ From Base:',
    style={'description_width': 'initial'},
    layout=widgets.Layout(width='300px')
)

output_base = widgets.Dropdown(
    options=bases.keys(),
    value='Binary',
    description='๐Ÿ“ค To Base:',
    style={'description_width': 'initial'},
    layout=widgets.Layout(width='300px')
)

output = widgets.Output()

# ๐Ÿ”„ Conversion Logic
def convert_number(change=None):
    output.clear_output()
    try:
        base_from = bases[input_base.value]
        base_to = bases[output_base.value]
        num_str = input_number.value.strip()

        # Convert input to decimal
        decimal_value = int(num_str, base_from)

        # Convert decimal to target base
        if base_to == 2:
            converted = bin(decimal_value)[2:]
        elif base_to == 8:
            converted = oct(decimal_value)[2:]
        elif base_to == 16:
            converted = hex(decimal_value)[2:]
        else:
            converted = str(decimal_value)

        with output:
            display(Markdown(f"### โœ… Result"))
            display(Markdown(f"**{input_base.value} {num_str} โ†’ {output_base.value} {converted}**"))
            display(Markdown(f"**๐Ÿ“˜ Interpretation:** Converted from base {base_from} to base {base_to} using Python's built-in conversion functions."))

    except ValueError:
        with output:
            display(Markdown("โŒ **Invalid input for the selected base. Please check your number format.**"))

# ๐Ÿ” Observe Changes
input_number.observe(convert_number, names='value')
input_base.observe(convert_number, names='value')
output_base.observe(convert_number, names='value')

# ๐Ÿš€ Display Widgets
display(widgets.HBox([input_base, output_base]))
display(input_number)
display(output)
convert_number()