{"record":{"id":"65c2c1ae455ac8e8","repo":"TheAlgorithms/Python","slug":"invalid-ipv4-octet-octet","errorCode":null,"errorMessage":"Invalid IPv4 octet {octet}","messagePattern":"Invalid IPv4 octet (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"conversions/ipv4_conversion.py","lineNumber":35,"sourceCode":"    167772415\n    >>> ipv4_to_decimal(\"10.0.255\")\n    Traceback (most recent call last):\n        ...\n    ValueError: Invalid IPv4 address format\n    >>> ipv4_to_decimal(\"10.0.0.256\")\n    Traceback (most recent call last):\n        ...\n    ValueError: Invalid IPv4 octet 256\n    \"\"\"\n\n    octets = [int(octet) for octet in ipv4_address.split(\".\")]\n    if len(octets) != 4:\n        raise ValueError(\"Invalid IPv4 address format\")\n\n    decimal_ipv4 = 0\n    for octet in octets:\n        if not 0 <= octet <= 255:\n            raise ValueError(f\"Invalid IPv4 octet {octet}\")  # noqa: EM102\n        decimal_ipv4 = (decimal_ipv4 << 8) + int(octet)\n\n    return decimal_ipv4\n\n\ndef alt_ipv4_to_decimal(ipv4_address: str) -> int:\n    \"\"\"\n    >>> alt_ipv4_to_decimal(\"192.168.0.1\")\n    3232235521\n    >>> alt_ipv4_to_decimal(\"10.0.0.255\")\n    167772415\n    \"\"\"\n    return int(\"0x\" + \"\".join(f\"{int(i):02x}\" for i in ipv4_address.split(\".\")), 16)\n\n\ndef decimal_to_ipv4(decimal_ipv4: int) -> str:\n    \"\"\"\n    Convert a decimal representation of an IP address to its IPv4 format.","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/ipv4_conversion.py#L17-L53","documentation":"Raised by ipv4_to_decimal(ipv4_address) in conversions/ipv4_conversion.py:35 when an octet parses as an integer but falls outside 0..255. The function checks each of the four parts after the count check, so a well-formed dotted quad with an out-of-range value ('10.0.0.256', '300.1.1.1', '-1.0.0.0' style negatives) raises ValueError(f\"Invalid IPv4 octet {octet}\") naming the offending octet.","triggerScenarios":"ipv4_to_decimal('10.0.0.256'), ipv4_to_decimal('999.999.999.999'), or octets computed arithmetically (base + offset) that overflow 255.","commonSituations":"Incrementing the last octet past 255 when generating IPs in a loop; user-typed addresses with typos; values from counters that wrap into the next /24 without carries.","solutions":["Clamp or wrap overflowing octets: carry into the previous octet when the last exceeds 255","Validate up front with ipaddress.IPv4Address(addr) and catch ValueError","If generating from integers, ensure the source int is already in 0..4294967295 and use decimal_to_ipv4 instead"],"exampleFix":"# before\nip = f'10.0.0.{250 + i}'  # i > 5 -> ValueError\nipv4_to_decimal(ip)\n\n# after\nimport ipaddress\nip = str(ipaddress.IPv4Address(ipaddress.IPv4Address('10.0.0.250') + i))\nipv4_to_decimal(ip)","handlingStrategy":"validation","validationCode":"parts = ipv4_address.split('.')\nif len(parts) != 4 or not all(p.isdigit() and 0 <= int(p) <= 255 for p in parts):\n    raise ValueError(f'invalid IPv4: {ipv4_address!r}')","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Carry overflow into higher octets when incrementing addresses","Use ipaddress.IPv4Address + int arithmetic for IP math","Validate with ipaddress before passing user-typed addresses"],"tags":["python","value-error","ipv4","network","range-check"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}