๐ข 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 |
|
Everyday arithmetic, finance |
Binary |
2 |
0, 1 |
|
Computers, digital circuits |
Octal |
8 |
0โ7 |
|
Unix file permissions, legacy code |
Hexadecimal |
16 |
0โ9, AโF |
|
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 only0
and1
, while hexadecimal includes lettersAโ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
โ Decimal42
Decimal
42
โ Hexadecimal2A
Decimal
42
โ Octal52
๐ 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 |
|
|
|
|
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#
Input Handling:
The user enters a number and selects its base (e.g., Decimal).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 Binaryoct()
for Octalhex()
for Hexadecimal
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()