forked from OmkarPathak/pygorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathascii.py
More file actions
56 lines (47 loc) · 1.39 KB
/
ascii.py
File metadata and controls
56 lines (47 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
ASCII
Conversions from ASCII to:
- base2
- base16
Author: Ian Doarn
"""
from pygorithm.binary.binary_utils import pad
from pygorithm.binary.base10 import to_base2 as b10_to_b2
from pygorithm.binary.base2 import to_base16 as b2_to_b16, \
to_ascii as b2_to_ascii
def to_base16(string, visualize=False):
"""
Convert ascii to hexadecimal
:param string: string to convert
:param visualize: Show process
:param as_string: return value as string not array
:return: hex representation of given string
"""
hex_array = []
for b_value in to_base2(string):
if visualize:
print("{} -> {}".format(
b2_to_ascii(b_value), b2_to_b16(b_value)
))
hex_array.append(b2_to_b16(b_value))
return hex_array
def to_base2(string, visualize=False, as_string=False):
"""
Convert ascii string to binary
:param string: Ascii string
:param visualize: Show process
:param as_string: join strings with a space as one large value
:return: array of binary numbers, or entire string
"""
_list = []
for x in string:
if visualize:
print("{} -> {} -> {}".format(
x, str(ord(x)),
str(b10_to_b2(ord(x)))
))
value = pad(str(b10_to_b2(ord(x))))
_list.append(value)
if as_string:
return ' '.join(_list)
return _list