-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Create ipv4_conversion.py #11008
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
Create ipv4_conversion.py #11008
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5af0ee1
Create ipconversion.py
its-tapas 8cb1561
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 5bf7146
Update conversions/ipconversion.py
cclauss 079dc13
Update ipconversion.py
cclauss aa280cc
Rename ipconversion.py to ipv4_conversion.py
cclauss 586fef3
forward_propagation(32, 450_000) # Was 10_000_000
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,62 @@ | ||
# https://www.geeksforgeeks.org/convert-ip-address-to-integer-and-vice-versa/ | ||
|
||
|
||
def ip_to_decimal(ip_address: str) -> int: | ||
""" | ||
Convert an IPv4 address to its decimal representation. | ||
|
||
Args: | ||
ip_address (str): A string representing an IPv4 address (e.g., "192.168.0.1"). | ||
|
||
Returns: | ||
int: The decimal representation of the IP address. | ||
|
||
>>> ip_to_decimal("192.168.0.1") | ||
3232235521 | ||
>>> ip_to_decimal("10.0.0.255") | ||
167772415 | ||
""" | ||
|
||
ip_parts = ip_address.split(".") | ||
if len(ip_parts) != 4: | ||
raise ValueError("Invalid IPv4 address format") | ||
|
||
decimal_ip = 0 | ||
for part in ip_parts: | ||
decimal_ip = (decimal_ip << 8) + int(part) | ||
|
||
return decimal_ip | ||
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.
|
||
|
||
|
||
def decimal_to_ip(decimal_ip: int) -> str: | ||
""" | ||
Convert a decimal representation of an IP address to its IPv4 format. | ||
|
||
Args: | ||
decimal_ip (int): An integer representing the decimal IP address. | ||
|
||
Returns: | ||
str: The IPv4 representation of the decimal IP address. | ||
|
||
>>> decimal_to_ip(3232235521) | ||
'192.168.0.1' | ||
>>> decimal_to_ip(167772415) | ||
'10.0.0.255' | ||
""" | ||
|
||
if not (0 <= decimal_ip <= 4294967295): | ||
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. This would not catch 10.0.0.256 which is not a valid IP. |
||
raise ValueError("Invalid decimal IP address") | ||
|
||
ip_parts = [] | ||
for _ in range(4): | ||
ip_parts.append(str(decimal_ip & 255)) | ||
decimal_ip >>= 8 | ||
|
||
ip_parts.reverse() | ||
return ".".join(ip_parts) | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.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.