-
-
Notifications
You must be signed in to change notification settings - Fork 46.7k
Added a hex-bin.py file in conversion.py #4433
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
Changes from 22 commits
1f6f4c6
9149d37
71cbcbc
9d4e6cb
e82db59
6549855
65d275b
fc99340
cbce9e1
e794056
4a36cad
9e212c6
63c2096
8d5c32a
6ba22e3
180f602
a5c49a1
95e9da1
77aec40
14a2d8b
350817e
e6abb38
62a52e2
c02fa6a
213e22a
e24b8a3
2d9cf63
a80cadd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# CONVERT HEXADECIMAL TO BINARY | ||
import math | ||
|
||
|
||
def hex_to_bin(hex_num: str) -> int: | ||
""" | ||
Convert a hexadecimal value to its decimal equivalent | ||
#https://stackoverflow.com/questions/1425493/convert-hex-to-binary | ||
Here, we have used " >> " bitwise operator | ||
Bitwise right shift: | ||
Shifts the bits of the number to the right and fills 0 on voids left as a result. | ||
Similar effect as of dividing the number with some power of two. | ||
Example: | ||
a = 10 | ||
a >> 1 = 5 | ||
|
||
>>> hex_to_bin("AC") | ||
10101100 | ||
>>> hex_to_bin("9A4") | ||
100110100100 | ||
>>> hex_to_bin(" 12f ") | ||
100101111 | ||
>>> hex_to_bin("FfFf") | ||
1111111111111111 | ||
>>> hex_to_bin("F-f") | ||
False | ||
>>> hex_to_bin("") | ||
False | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function returns an int or it raises an Exception. |
||
""" | ||
|
||
bin_str: str = "" | ||
hex_str: str = hex_num.strip() | ||
|
||
if not hex_str: | ||
return False | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as below. Simply raise a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function returns an int or it raises an Exception. |
||
|
||
is_negative: bool = hex_str[0] == "-" | ||
|
||
if is_negative: | ||
hex_str = hex_str[1:] | ||
try: | ||
int_num: int = int(hex_str, 16) | ||
except ValueError: | ||
return False | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of implicitly silencing the error as mentioned by @cclauss please use the previous format of |
||
|
||
while int_num > 0: | ||
str_num: str = str(int_num % 2) | ||
bin_str = str_num + bin_str | ||
int_num = int_num >> 1 | ||
|
||
return int("-" + "".join(bin_str)) if is_negative else int(bin_str) | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function returns an int or it raises an Exception.