-
-
Notifications
You must be signed in to change notification settings - Fork 50.4k
Expand file tree
/
Copy pathbinary_to_hexadecimal.py
More file actions
38 lines (30 loc) · 1.07 KB
/
binary_to_hexadecimal.py
File metadata and controls
38 lines (30 loc) · 1.07 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
def bin_to_hexadecimal(binary_str: str) -> str:
"""
Convert a binary string to hexadecimal.
>>> bin_to_hexadecimal('101011111')
'0x15f'
>>> bin_to_hexadecimal(' 1010 ')
'0xa'
>>> bin_to_hexadecimal('-11101')
'-0x1d'
>>> bin_to_hexadecimal('a')
Traceback (most recent call last):
...
ValueError: Non-binary value was passed to the function
>>> bin_to_hexadecimal('')
Traceback (most recent call last):
...
ValueError: Empty string was passed to the function
"""
binary_str = str(binary_str).strip()
if not binary_str:
raise ValueError("Empty string was passed to the function")
is_negative = binary_str[0] == "-"
binary_str = binary_str[1:] if is_negative else binary_str
if not binary_str or not all(char in "01" for char in binary_str):
raise ValueError("Non-binary value was passed to the function")
hex_str = "0x" + hex(int(binary_str, 2))[2:]
return "-" + hex_str if is_negative else hex_str
if __name__ == "__main__":
from doctest import testmod
testmod()