Skip to content

Commit 5a8670a

Browse files
hacktoberfest contribution - binary to hexadecimal (#31)
* hacktoberfest contribution - binary to hexadecimal * Adding regexp to detect invalid expressions and unit test cases
1 parent a7bfe0e commit 5a8670a

File tree

2 files changed

+228
-1
lines changed

2 files changed

+228
-1
lines changed

easyPythonpi/TestBin2Hex.py

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import unittest
2+
import easyPythonpi
3+
4+
class TestBin2Hex(unittest.TestCase):
5+
6+
def test_single_binary_zero(self):
7+
self.assertEqual( easyPythonpi.bin2hex('0'), '0')
8+
9+
def test_single_binary_one(self):
10+
self.assertEqual( easyPythonpi.bin2hex('1'), '1')
11+
12+
def test_binary_two_zeros(self):
13+
self.assertEqual( easyPythonpi.bin2hex('00'), '0')
14+
15+
def test_binary_three_zeros(self):
16+
self.assertEqual( easyPythonpi.bin2hex('000'), '0')
17+
18+
def test_binary_four_zeros(self):
19+
self.assertEqual( easyPythonpi.bin2hex('0000'), '0')
20+
21+
def test_binary_1111_to_hex(self):
22+
self.assertEqual( easyPythonpi.bin2hex('1111'), 'F')
23+
24+
def test_binary_1110_to_hex(self):
25+
self.assertEqual( easyPythonpi.bin2hex('1110'), 'E')
26+
27+
def test_binary_1101_to_hex(self):
28+
self.assertEqual( easyPythonpi.bin2hex('1101'), 'D')
29+
30+
def test_binary_1100_to_hex(self):
31+
self.assertEqual( easyPythonpi.bin2hex('1100'), 'C')
32+
33+
def test_binary_1011_to_hex(self):
34+
self.assertEqual( easyPythonpi.bin2hex('1011'), 'B')
35+
36+
def test_binary_1010_to_hex(self):
37+
self.assertEqual( easyPythonpi.bin2hex('1010'), 'A')
38+
39+
def test_binary_1001_to_hex(self):
40+
self.assertEqual( easyPythonpi.bin2hex('1001'), '9')
41+
42+
def test_binary_1000_to_hex(self):
43+
self.assertEqual( easyPythonpi.bin2hex('1000'), '8')
44+
45+
def test_binary_0111_to_hex(self):
46+
self.assertEqual( easyPythonpi.bin2hex('0111'), '7')
47+
48+
def test_binary_111_to_hex(self):
49+
self.assertEqual( easyPythonpi.bin2hex('111'), '7')
50+
51+
def test_binary_0110_to_hex(self):
52+
self.assertEqual( easyPythonpi.bin2hex('0110'), '6')
53+
54+
def test_binary_110_to_hex(self):
55+
self.assertEqual( easyPythonpi.bin2hex('110'), '6')
56+
57+
def test_binary_0101_to_hex(self):
58+
self.assertEqual( easyPythonpi.bin2hex('0101'), '5')
59+
60+
def test_binary_101_to_hex(self):
61+
self.assertEqual( easyPythonpi.bin2hex('101'), '5')
62+
63+
def test_binary_0100_to_hex(self):
64+
self.assertEqual( easyPythonpi.bin2hex('0100'), '4')
65+
66+
def test_binary_100_to_hex(self):
67+
self.assertEqual( easyPythonpi.bin2hex('100'), '4')
68+
69+
def test_binary_0011_to_hex(self):
70+
self.assertEqual( easyPythonpi.bin2hex('0011'), '3')
71+
72+
def test_binary_011_to_hex(self):
73+
self.assertEqual( easyPythonpi.bin2hex('011'), '3')
74+
75+
def test_binary_11_to_hex(self):
76+
self.assertEqual( easyPythonpi.bin2hex('11'), '3')
77+
78+
def test_binary_0010_to_hex(self):
79+
self.assertEqual( easyPythonpi.bin2hex('0010'), '2')
80+
81+
def test_binary_010_to_hex(self):
82+
self.assertEqual( easyPythonpi.bin2hex('010'), '2')
83+
84+
def test_binary_10_to_hex(self):
85+
self.assertEqual( easyPythonpi.bin2hex('10'), '2')
86+
87+
def test_binary_01_to_hex(self):
88+
self.assertEqual( easyPythonpi.bin2hex('01'), '1')
89+
90+
def test_binary_110100001111_to_hex(self):
91+
self.assertEqual( easyPythonpi.bin2hex('110100001111'), 'D0F')
92+
93+
def test_binary_0100011111_to_hex(self):
94+
self.assertEqual( easyPythonpi.bin2hex('0100011111'), '11F')
95+
96+
def test_invalid_binary_A(self):
97+
with self.assertRaises(easyPythonpi.InvalidBinaryException):
98+
easyPythonpi.bin2hex('A')
99+
100+
def test_invalid_binary_123(self):
101+
with self.assertRaises(easyPythonpi.InvalidBinaryException):
102+
easyPythonpi.bin2hex('123')
103+
104+
def test_invalid_binary_0101A1000100(self):
105+
with self.assertRaises(easyPythonpi.InvalidBinaryException):
106+
easyPythonpi.bin2hex('0101A1000100')
107+
108+
def test_invalid_binary_A101100010B(self):
109+
with self.assertRaises(easyPythonpi.InvalidBinaryException):
110+
easyPythonpi.bin2hex('A101100010B')
111+
112+
def test_invalid_binary_nonalphanumeric(self):
113+
with self.assertRaises(easyPythonpi.InvalidBinaryException):
114+
easyPythonpi.bin2hex('!')
115+
116+
def test_invalid_binary_nonalphanumeric_in_binary(self):
117+
with self.assertRaises(easyPythonpi.InvalidBinaryException):
118+
easyPythonpi.bin2hex('001~')
119+
120+
def test_invalid_binary_subtraction(self):
121+
with self.assertRaises(easyPythonpi.InvalidBinaryException):
122+
easyPythonpi.bin2hex('0000-1000')
123+
124+
def test_invalid_binary_anding(self):
125+
with self.assertRaises(easyPythonpi.InvalidBinaryException):
126+
easyPythonpi.bin2hex('0000&1000')
127+
128+
def test_invalid_helloWorld_expression(self):
129+
with self.assertRaises(easyPythonpi.InvalidBinaryException):
130+
easyPythonpi.bin2hex('hello world')
131+
132+
def test_invalid_regexpression(self):
133+
with self.assertRaises(easyPythonpi.InvalidBinaryException):
134+
easyPythonpi.bin2hex('[0-1]')
135+
136+
137+
# class TestBin2Octal(unittest.TestCase):
138+
139+
if __name__ == '__main__':
140+
unittest.main()

easyPythonpi/easyPythonpi.py

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import regex as re
12
""" A python module that helps you to calculate some of the most used calculations.....
23
usage--
34
Just download the file from git and unzip in ur system.
@@ -128,7 +129,87 @@ def bin2dec(x:'bin')->'dec':
128129

129130
a=a+1
130131
return r
131-
132+
133+
# A function that converts a binary string to hexadecimal
134+
def bin2hex(x:'bin')->'hex':
135+
"""Converts a binary string to a hexadecimal string.
136+
137+
This function converts a binary string to hexadecimal. If the binary
138+
string's length is a multiple of 4, simply break up the string into
139+
substrings of 4 binary digits and determine their hexadecimal digit.
140+
If the binary string's length not a multiple of 4 prepend the
141+
necessary number of 0's to make the string a multiple of 4.
142+
143+
Args:
144+
x ('bin') : Binary string to convert to hexadecimal.
145+
146+
Returns:
147+
The binary converted to hexadecimal.
148+
149+
Raises:
150+
No errors
151+
"""
152+
153+
h = '' # hexadecimal number converted from binary and to be returned
154+
155+
156+
x=str(x)
157+
158+
# Determine if the string has invalid characters
159+
if re.search('[^(0-1)]', x):
160+
raise InvalidBinaryException
161+
162+
# Get the length of the string
163+
l=len(x)
164+
165+
# Begin the process of converting x to its hexadecimal number
166+
167+
# If the length is not a multiple of 4, prepend 0's before converting
168+
if l % 4 != 0:
169+
numZerosPrepended = 4 - ( l % 4 ) # number of zeros to prepend
170+
x = (numZerosPrepended * '0') + x # concatenate numZerosPrepended to x
171+
172+
173+
for i in range(len(x)-1, 0, -4):
174+
substring = x[i-3:i+1] # The substring converted to a hex character
175+
176+
# Determine the binary substring's hex and prepend to h
177+
if substring == '0000':
178+
h = '0' + h
179+
elif substring == '0001':
180+
h = '1' + h
181+
elif substring == '0010':
182+
h = '2' + h
183+
elif substring == '0011':
184+
h = '3' + h
185+
elif substring == '0100':
186+
h = '4' + h
187+
elif substring == '0101':
188+
h = '5' + h
189+
elif substring == '0110':
190+
h = '6' + h
191+
elif substring == '0111':
192+
h = '7' + h
193+
elif substring == '1000':
194+
h = '8' + h
195+
elif substring == '1001':
196+
h = '9' + h
197+
elif substring == '1010':
198+
h = 'A' + h
199+
elif substring == '1011':
200+
h = 'B' + h
201+
elif substring == '1100':
202+
h = 'C' + h
203+
elif substring == '1100':
204+
h = 'C' + h
205+
elif substring == '1101':
206+
h = 'D' + h
207+
elif substring == '1110':
208+
h = 'E' + h
209+
elif substring == '1111':
210+
h = 'F' + h
211+
212+
return h
132213

133214
def createarray(length:'int',dtype='int')->'array': # To create an array of entered length and entered data type(interger data type is a default data type)
134215
import numpy as np
@@ -333,3 +414,9 @@ def count_vowels(ip_str:'str')->'int':
333414
#return the count dictionary
334415
return count
335416

417+
418+
# Programmer defined exceptions go here:
419+
420+
# define exception for invalid Binary Strings
421+
class InvalidBinaryException(Exception):
422+
pass

0 commit comments

Comments
 (0)