-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Create hexadecimal_to_decimal #2393
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
763d1b1
Create hexadecimal_to_decimal
mohammadreza490 dced099
Update conversions/hexadecimal_to_decimal
mohammadreza490 1b4db3f
Update conversions/hexadecimal_to_decimal
mohammadreza490 61aa53d
Update conversions/hexadecimal_to_decimal
mohammadreza490 1579cf8
Update hexadecimal_to_decimal
mohammadreza490 1365270
Update hexadecimal_to_decimal
mohammadreza490 664adb6
Update hexadecimal_to_decimal
mohammadreza490 234e4bd
Update hexadecimal_to_decimal
mohammadreza490 2678952
Update hexadecimal_to_decimal
mohammadreza490 ee8edad
Update conversions/hexadecimal_to_decimal
mohammadreza490 1889cc7
Update hexadecimal_to_decimal
mohammadreza490 cd6eac3
Update hexadecimal_to_decimal
mohammadreza490 8f35319
Update conversions/hexadecimal_to_decimal
mohammadreza490 a361d92
Update conversions/hexadecimal_to_decimal
mohammadreza490 8c680cc
Update hexadecimal_to_decimal
mohammadreza490 7bf7698
Update hexadecimal_to_decimal
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
hex_table = {hex(i)[2:]: i for i in range(16)} # Use [:2] to strip off the leading '0x' | ||
|
||
|
||
def hex_to_decimal(hex_string: str) -> int: | ||
""" | ||
Convert a hexadecimal value to its decimal equivalent | ||
#https://www.programiz.com/python-programming/methods/built-in/hex | ||
|
||
>>> hex_to_decimal("a") | ||
10 | ||
>>> hex_to_decimal("12f") | ||
303 | ||
>>> hex_to_decimal(" 12f ") | ||
303 | ||
>>> hex_to_decimal("FfFf") | ||
65535 | ||
>>> hex_to_decimal("-Ff") | ||
-255 | ||
>>> hex_to_decimal("F-f") | ||
ValueError: Non-hexadecimal value was passed to the function | ||
>>> hex_to_decimal("") | ||
ValueError: Empty string value was passed to the function | ||
>>> hex_to_decimal("12m") | ||
ValueError: Non-hexadecimal value was passed to the function | ||
""" | ||
hex_string = hex_string.strip().lower() | ||
if not hex_string: | ||
raise ValueError("Empty string was passed to the function") | ||
is_negative = hex_string[0] == "-" | ||
if is_negative: | ||
hex_string = hex_string[1:] | ||
if not all(char in hex_table for char in hex_string): | ||
raise ValueError("Non-hexadecimal value was passed to the function") | ||
decimal_number = 0 | ||
for char in hex_string: | ||
decimal_number = 16 * decimal_number + hex_table[char] | ||
if is_negative: | ||
decimal_number = -decimal_number | ||
return decimal_number | ||
|
||
|
||
if __name__ == "__main__": | ||
from doctest import testmod | ||
|
||
testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.