Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script to convert a binary to ASCII or to decimal #221 #222

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Binary Conversion/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Binary Conversion

This Python script performs the conversion of a binary into an ASCII code or into a decimal

There is no requirements to use it, because the imported module `typing` used for the code is a built-in one.
68 changes: 68 additions & 0 deletions Binary Conversion/base_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""
Binary Conversion
Author: @tonybnya
Filename: base_2.py

A Python script to convert a string representation of a binary number
into its ASCII code, or into its decimal
"""
from typing import List, Union


def into_ascii(binary: Union[str, List[str]], visual: bool = False) -> str:
"""
Binary to ASCII

:param binary: binary number or an array
:param visual: process visualization
:return: ASCII code as a string
"""

if not isinstance(binary, str) or not isinstance(binary, list):
raise ValueError(
f"Given value must be a string or an array of strings, \
not type {str(type(binary))}"
)
if isinstance(binary, str):
digits = binary.split()
elif isinstance(binary, list):
digits = binary

code = ""

for digit in digits:
if visual:
print(
f"{digit} -> {into_base_10(int(digit))} -> \
{chr(into_base_10(int(digit)))}"
)

value = into_base_10(int(digit))
code += chr(value)

return code


def into_base_10(binary: int, visual: bool = False) -> int:
"""
Binary to decimal

:param binary: binary number
:param visual: process visualization
:return: a decimal number
"""
if not isinstance(binary, int):
raise ValueError(
f"Given value must be an integer, not type {str(type(binary))}"
)

num = 0
lst = [int(i) for i in str(binary)]

for digit in lst:
if visual:
print(f"{str(num)} x 2 + {str(digit)} = {str(num * 2 + digit)}")

num = num * 2 + digit

return num
5 changes: 5 additions & 0 deletions Decimal Conversion/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Decimal Conversion

This Python script performs the conversion of a decimal into a binary or into an hexadecimal

There is no requirements to use it.
83 changes: 83 additions & 0 deletions Decimal Conversion/base_10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"""
Decimal Conversion
Author: @tonybnya
Filename: base_10.py

A Python script to convert a decimal to binary or to hexadecimal
"""
HEXADECIMALS = {
0: '0',
1: '1',
2: '2',
3: '3',
4: '4',
5: '5',
6: '6',
7: '7',
8: '8',
9: '9',
10: 'A',
11: 'B',
12: 'C',
13: 'D',
14: 'E',
15: 'F'
}


def into_base_2(num: int, visual: bool = False) -> int:
"""
Decimal to binary

:param num: decimal number
:param visual: process visualization
:return: binary number
"""
if not isinstance(num, int):
raise ValueError(
f"Given value must be an integer, not type {str(type(num))}"
)

lst = []
while num > 0.5:
if visual:
print(
f"{str(num)} / 2 = {str(num / 2)} || \
{str(num)} % 2 = {str(num % 2)}"
)

lst.append(num % 2)
num = int(num / 2)

return int(''.join([str(i) for i in lst[::-1]]))


def into_base_16(num: int, visual: bool = False) -> str:
"""
Decimal to hexadecimal

:param num: decimal number
:param visual: process visualization
:return: hexadecimal number
"""
if not isinstance(num, int):
raise ValueError(
f"Given value must be an integer, not type {str(type(num))}"
)

lst = []
while num != 0:
if visual:
print(
f"{str(num)} % 16 = {str(n % 16)} -> \
hex = {HEXADECIMALS[num % 16]}"
)

lst.append(HEXADECIMALS[num % 16])
num = int(num / 16)

if visual:
print(lst)
print(f"reversed = {str(lst[::-1])}")

return ''.join(lst[::-1])
Loading