Skip to content

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 6 commits into from
Oct 28, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions conversions/ipconversion.py
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def alt_ip_to_decimal(ip_address: str) -> int:
    return int("0x" + "".join(f"{int(i):02x}" for i in ip_address.split(".")), 16)



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):
Copy link
Member

Choose a reason for hiding this comment

The 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()