Skip to content

Commit 31ae442

Browse files
author
IanDoarn
committed
Added binary conversions
1 parent 9aa3851 commit 31ae442

13 files changed

+503
-2
lines changed

pygorithm/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,12 @@
5757
]
5858

5959
__all__ = [
60+
'binary',
6061
'data_structures',
6162
'fibonacci',
6263
'math',
6364
'searching',
6465
'sorting',
6566
'string',
66-
'pathfinding',
67+
'pathfinding'
6768
]

pygorithm/binary/__init__.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
Collection or binary conversions and algorithms
3+
"""
4+
from . import base2_to_base10
5+
from . import base2_to_base16
6+
from . import base2_to_ascii
7+
from . import base10_to_base2
8+
from . import base10_to_base16
9+
from . import base16_to_base2
10+
from . import base16_to_base10
11+
from . import base16_to_ascii
12+
from . import ascii_to_base2
13+
from . import ascii_to_base16
14+
15+
__all__ = [
16+
'ascii_to_base2',
17+
'ascii_to_base16',
18+
'base2_to_base10',
19+
'base2_to_base16',
20+
'base2_to_ascii',
21+
'base10_to_base2',
22+
'base10_to_base16',
23+
'base16_to_base10',
24+
'base16_to_base2',
25+
'base16_to_ascii'
26+
]

pygorithm/binary/ascii_to_base16.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
ASCII to Hexadecimal
3+
4+
Author: Ian Doarn
5+
"""
6+
from pygorithm.binary.ascii_to_base2 import convert_ascii_to_base2
7+
from pygorithm.binary.base2_to_base16 import convert_base2_to_base16
8+
from pygorithm.binary.base2_to_ascii import convert_base2_to_ascii
9+
10+
11+
def convert_ascii_to_base16(string, visualize=False):
12+
"""
13+
Convert ascii to hexadecimal
14+
15+
:param string: string to convert
16+
:param visualize: Show process
17+
:param as_string: return value as string not array
18+
:return: hex representation of given string
19+
"""
20+
hex_array = []
21+
22+
for b_value in convert_ascii_to_base2(string):
23+
if visualize:
24+
print("{} -> {}".format(
25+
convert_base2_to_ascii(b_value), convert_base2_to_base16(b_value)
26+
))
27+
hex_array.append(convert_base2_to_base16(b_value))
28+
29+
return hex_array

pygorithm/binary/ascii_to_base2.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
ASCII to Binary
3+
4+
Author: Ian Doarn
5+
"""
6+
from pygorithm.binary.base10_to_base2 import convert_base10_to_base2
7+
8+
9+
def convert_ascii_to_base2(string, visualize=False, as_string=False):
10+
"""
11+
Convert ascii string to binary
12+
13+
:param string: Ascii string
14+
:param visualize: Show process
15+
:param as_string: join strings with a space as one large value
16+
:return: array of binary numbers, or entire string
17+
"""
18+
_list = []
19+
for x in string:
20+
if visualize:
21+
print("{} -> {} -> {}".format(
22+
x, str(ord(x)),
23+
str(convert_base10_to_base2(ord(x)))
24+
))
25+
_list.append(str(convert_base10_to_base2(ord(x))))
26+
27+
if as_string:
28+
return ' '.join(_list)
29+
return _list

pygorithm/binary/base10_to_base16.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
Convert decimal numbers to hexadecimal numbers
3+
4+
Author: Ian Doarn
5+
"""
6+
7+
8+
hex_values = {
9+
0: '0', 1: '1', 2: '2',
10+
3: '3', 4: '4', 5: '5',
11+
6: '6', 7: '7', 8: '8',
12+
9: '9', 10: 'A', 11: 'B',
13+
12: 'C', 13: 'D', 14: 'E',
14+
15: 'F'
15+
}
16+
17+
18+
def convert_base10_to_base16(n, visualize=False):
19+
"""
20+
Convert decimal number to hexadecimal
21+
22+
Divide the number by 16 and add the remainder
23+
to a list, round down the value after division
24+
and repeat till our value is 0
25+
26+
Reverse the results list, get each values respective
27+
hex value using hex_values map
28+
29+
:param n: decimal number
30+
:param visualize: Show process
31+
:return: hexadecimal number
32+
"""
33+
_list = []
34+
while n != 0:
35+
if visualize:
36+
print("{} % 16 = {} -> hex = {}".format(
37+
str(n), str(n % 16), hex_values[n % 16]
38+
))
39+
_list.append(hex_values[n % 16])
40+
n = int(n / 16)
41+
42+
if visualize:
43+
print(_list)
44+
print("reversed = " + str(_list[::-1]))
45+
46+
return ''.join(_list[::-1])

pygorithm/binary/base10_to_base2.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
Base10 to Base2
3+
4+
Convert decimal numbers to binary numbers
5+
6+
Author: Ian Doarn
7+
"""
8+
9+
10+
def convert_base10_to_base2(n, visualize=False):
11+
"""
12+
Divide each number by 2 using
13+
the % operator.
14+
15+
Reverse the resulting list of numbers
16+
and return the result
17+
18+
:param n: decimal number
19+
:param visualize: Show process
20+
:return: binary number
21+
"""
22+
_list = []
23+
while n > 0.5:
24+
if visualize:
25+
print("{} / 2 = {} || {} % 2 = {}".format(str(n), str(n / 2), str(n), str(n % 2)))
26+
_list.append(n % 2)
27+
n = int(n / 2)
28+
29+
# Reverse the list, turn each number to a string,
30+
# join the string, and convert it back to an integer
31+
return int(''.join([str(i) for i in _list[::-1]]))

pygorithm/binary/base16_to_ascii.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
Hexadecimal to ASCII
3+
4+
Author: Ian Doarn
5+
"""
6+
from pygorithm.binary.base16_to_base2 import convert_base16_to_base2
7+
from pygorithm.binary.base2_to_ascii import convert_base2_to_ascii
8+
9+
10+
def convert_base16_to_ascii(h_array, visualize=False):
11+
"""
12+
Convert base16 array to ASCII string
13+
14+
Input must be a list of strings:
15+
Example::
16+
17+
array = [
18+
'74', '68', '65',
19+
'20', '71', '75',
20+
'69', '63', '6B',
21+
'20', '62', '72',
22+
'6F', '77', '6E',
23+
'20', '66', '6F',
24+
'78', '20', '6A',
25+
'75', '6D', '70',
26+
'73', '20', '6F',
27+
'76', '65', '72',
28+
'20', '74', '68',
29+
'65', '20', '6C',
30+
'61', '7A', '79',
31+
'20', '64', '6F',
32+
'67'
33+
]
34+
35+
result -> the quick brown fox jumps over the lazy dog
36+
37+
:param h_array: hex value array
38+
:param visualize: Show process
39+
:return: ASCII string
40+
"""
41+
string = ''
42+
43+
for h_value in h_array:
44+
if visualize:
45+
print("{} -> {} -> {}".format(
46+
h_value,
47+
convert_base16_to_base2(h_value),
48+
convert_base2_to_ascii(str(convert_base16_to_base2(h_value)))
49+
))
50+
51+
string += convert_base2_to_ascii(str(convert_base16_to_base2(h_value)))
52+
53+
return string

pygorithm/binary/base16_to_base10.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
Hexadecimal to Decimal
3+
4+
Author: Ian Doarn
5+
"""
6+
from math import pow
7+
8+
hex_letter_values = {
9+
'0': 0, '1': 1, '2': 2,
10+
'3': 3, '4': 4, '5': 5,
11+
'6': 6, '7': 7, '8': 8,
12+
'9': 9, 'A': 10, 'B': 11,
13+
'C': 12, 'D': 13, 'E': 14,
14+
'F': 15
15+
}
16+
17+
18+
def convert_base16_to_base10(h, visualize=False):
19+
"""
20+
Convert hexadecimal number to decimal number
21+
22+
Send hex to a list and reverse. Evaluate each hex value
23+
via hex_letter_values map. Enumerate the list,
24+
25+
using the equation: value * 16 ^ index
26+
27+
value is the hex char value: F -> 15
28+
index is its position in the list: ['1', 'A', 'F'] F's index = 2
29+
30+
Continue this for each hex letter until we reach the end of the list,
31+
summing all evaluated values.
32+
33+
:param h: hexadecimal number
34+
:param visualize: Show process
35+
:return: decimal number
36+
"""
37+
38+
# Check to see if '0x' is at the beginning and remove it
39+
if h[0:2] == '0x':
40+
hex_char_list = list(h[2:])[::-1]
41+
else:
42+
hex_char_list = list(h)[::-1]
43+
44+
value = 0
45+
46+
for i, v in enumerate(hex_char_list):
47+
if visualize:
48+
print("{} -> {} || {} * (16 ^ {}) = {}".format(
49+
v, str(hex_letter_values[v]),
50+
str(hex_letter_values[v]),
51+
str(i),
52+
str(hex_letter_values[v] * (pow(16, i)))
53+
))
54+
55+
value += hex_letter_values[v] * (pow(16, i))
56+
57+
return int(value)

pygorithm/binary/base16_to_base2.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Hexadecimal to Binary
3+
4+
Author: Ian Doarn
5+
"""
6+
7+
hex_binary_values = {
8+
'0': '0000', '1': '0001', '2': '0010', '3': '0011',
9+
'4': '0100', '5': '0101', '6': '0110', '7': '0111',
10+
'8': '1000', '9': '1001', 'A': '1010', 'B': '1011',
11+
'C': '1100', 'D': '1101', 'E': '1110', 'F': '1111'
12+
}
13+
14+
15+
def convert_base16_to_base2(h, visualize=False):
16+
"""
17+
Convert hexadecimal to binary number
18+
19+
:param h: hexadecimal number
20+
:param visualize: Show process
21+
:return: binary number
22+
"""
23+
24+
hex_char_list = list(h)
25+
_list = []
26+
27+
for value in hex_char_list:
28+
if visualize:
29+
print("{} -> {}".format(
30+
value, hex_binary_values[value]
31+
))
32+
_list.append(hex_binary_values[value])
33+
34+
return int(''.join(_list))

pygorithm/binary/base2_to_ascii.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Base2 to ASCII
3+
4+
Author: Ian Doarn
5+
"""
6+
from pygorithm.binary.base2_to_base10 import convert_base2_to_base10
7+
8+
9+
def convert_base2_to_ascii(b, visualize=False):
10+
"""
11+
Convert binary to ASCII
12+
13+
:param b: binary number or array
14+
:param visualize: Show process
15+
:return: ASCII String
16+
"""
17+
binary = b
18+
# Make sure give binary is a str array
19+
if type(b) is str:
20+
binary = b.split(' ')
21+
elif type(b) is list:
22+
binary = b
23+
24+
string = ''
25+
26+
for b_value in binary:
27+
if visualize:
28+
print("{} -> {} -> {}".format(
29+
b_value, convert_base2_to_base10(int(b_value)),
30+
chr(convert_base2_to_base10(int(b_value)))
31+
))
32+
value = convert_base2_to_base10(int(b_value))
33+
string += chr(value)
34+
return string

0 commit comments

Comments
 (0)