-
-
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 9 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,67 @@ | ||||
#convert hexadecimal into binary | ||||
from doctest import testmod | ||||
import math | ||||
|
||||
def convert(num: str) -> str: | ||||
|
||||
""" | ||||
Convert a hexadecimal value to its decimal equivalent | ||||
#https://stackoverflow.com/questions/1425493/convert-hex-to-binary | ||||
>>> convert("AC") | ||||
10101100 | ||||
>>> convert("9A4") | ||||
100110100100 | ||||
>>> convert(" 12f ") | ||||
100101111 | ||||
>>> convert("FfFf") | ||||
1111111111111111 | ||||
>>> convert("F-f") | ||||
Traceback (most recent call last): | ||||
... | ||||
ValueError: invalid literal for int() with base 16: | ||||
>>> convert() | ||||
Traceback (most recent call last): | ||||
... | ||||
ValueError: Empty string was passed to the function: | ||||
|
||||
""" | ||||
|
||||
bin_str : str="" | ||||
flag :bool = 0 | ||||
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.
Suggested change
Let's just use 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. That is sick!!! |
||||
hex_str :str = num.strip() | ||||
|
||||
if not hex_str: | ||||
raise ValueError("Empty string was passed to the function") | ||||
onlinejudge95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
is_negative : bool= hex_str[0] == "-" | ||||
|
||||
if is_negative: | ||||
flag = 1 | ||||
hex_string = hex_str[1:] | ||||
|
||||
num2 :int = int(hex_str, 16) | ||||
onlinejudge95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
|
||||
""" | ||||
Here, we have used " >> " bitwise operator | ||||
onlinejudge95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
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 | ||||
|
||||
""" | ||||
|
||||
while num2>0: | ||||
onlinejudge95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
num3 :str = str(num2%2) | ||||
onlinejudge95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
bin_str = num3 + bin_str | ||||
num2=num2>>1 | ||||
onlinejudge95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
|
||||
if flag: | ||||
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. One last change, maybe we can make this return x if condition else y |
||||
return "-"+bin_str | ||||
onlinejudge95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
else: | ||||
return bin_str | ||||
|
||||
|
||||
if __name__ == "__main__": | ||||
testmod(name = 'convert', verbose = True) | ||||
|
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.
Also remove this whitespace