Convert an IPv4 address to its decimal representation. Args: ip_address: A string representing an IPv4 address (e.g., "192.168.0.1"). Returns: int: The decimal representation of the IP address. >>> ipv4_to_decimal("192.168.0.1") 3232235521 >>> ipv4_to_deci
(ipv4_address: str)
| 2 | |
| 3 | |
| 4 | def ipv4_to_decimal(ipv4_address: str) -> int: |
| 5 | """ |
| 6 | Convert an IPv4 address to its decimal representation. |
| 7 | |
| 8 | Args: |
| 9 | ip_address: A string representing an IPv4 address (e.g., "192.168.0.1"). |
| 10 | |
| 11 | Returns: |
| 12 | int: The decimal representation of the IP address. |
| 13 | |
| 14 | >>> ipv4_to_decimal("192.168.0.1") |
| 15 | 3232235521 |
| 16 | >>> ipv4_to_decimal("10.0.0.255") |
| 17 | 167772415 |
| 18 | >>> ipv4_to_decimal("10.0.255") |
| 19 | Traceback (most recent call last): |
| 20 | ... |
| 21 | ValueError: Invalid IPv4 address format |
| 22 | >>> ipv4_to_decimal("10.0.0.256") |
| 23 | Traceback (most recent call last): |
| 24 | ... |
| 25 | ValueError: Invalid IPv4 octet 256 |
| 26 | """ |
| 27 | |
| 28 | octets = [int(octet) for octet in ipv4_address.split(".")] |
| 29 | if len(octets) != 4: |
| 30 | raise ValueError("Invalid IPv4 address format") |
| 31 | |
| 32 | decimal_ipv4 = 0 |
| 33 | for octet in octets: |
| 34 | if not 0 <= octet <= 255: |
| 35 | raise ValueError(f"Invalid IPv4 octet {octet}") # noqa: EM102 |
| 36 | decimal_ipv4 = (decimal_ipv4 << 8) + int(octet) |
| 37 | |
| 38 | return decimal_ipv4 |
| 39 | |
| 40 | |
| 41 | def alt_ipv4_to_decimal(ipv4_address: str) -> int: |