{"record":{"id":"a6fa64f86f1ac632","repo":"TheAlgorithms/Python","slug":"invalid-decimal-ipv4-address","errorCode":null,"errorMessage":"Invalid decimal IPv4 address","messagePattern":"Invalid decimal IPv4 address","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"conversions/ipv4_conversion.py","lineNumber":72,"sourceCode":"\n    Args:\n        decimal_ipv4: An integer representing the decimal IP address.\n\n    Returns:\n        The IPv4 representation of the decimal IP address.\n\n    >>> decimal_to_ipv4(3232235521)\n    '192.168.0.1'\n    >>> decimal_to_ipv4(167772415)\n    '10.0.0.255'\n    >>> decimal_to_ipv4(-1)\n    Traceback (most recent call last):\n        ...\n    ValueError: Invalid decimal IPv4 address\n    \"\"\"\n\n    if not (0 <= decimal_ipv4 <= 4294967295):\n        raise ValueError(\"Invalid decimal IPv4 address\")\n\n    ip_parts = []\n    for _ in range(4):\n        ip_parts.append(str(decimal_ipv4 & 255))\n        decimal_ipv4 >>= 8\n\n    return \".\".join(reversed(ip_parts))\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":54,"sourceCodeEnd":86,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/ipv4_conversion.py#L54-L86","documentation":"Raised by decimal_to_ipv4(decimal_ipv4) in conversions/ipv4_conversion.py:72 when the integer is outside 0..4294967295 (2**32 - 1). An IPv4 address is exactly 32 bits; negative numbers or values above the unsigned 32-bit maximum cannot map to one, so the function raises ValueError('Invalid decimal IPv4 address') before extracting octets.","triggerScenarios":"decimal_to_ipv4(-1), decimal_to_ipv4(2**32), or decimal values produced by signed 32-bit arithmetic, struct unpacking with signed formats, or unchecked increments.","commonSituations":"Reading 32-bit words unpacked as signed integers ('<i' instead of '<I'); converting hash/CRC values that exceed 2**32; off-by-one loop bounds when enumerating address ranges.","solutions":["Mask to 32 bits before calling: decimal_to_ipv4(value & 0xFFFFFFFF) when wrapping is the intended semantics","Unpack network words as unsigned: struct.unpack('<I', data) rather than '<i'","Range-check at your boundary: if not 0 <= value <= 4294967295: reject"],"exampleFix":"# before\nsigned, = struct.unpack('<i', word)\ndecimal_to_ipv4(signed)  # negative -> ValueError\n\n# after\nunsigned, = struct.unpack('<I', word)\ndecimal_to_ipv4(unsigned)","handlingStrategy":"validation","validationCode":"if not 0 <= decimal_ipv4 <= 0xFFFFFFFF:\n    raise ValueError(f'{decimal_ipv4} is not an unsigned 32-bit value')","typeGuard":"def is_u32(v: object) -> TypeGuard[int]:\n    return isinstance(v, int) and 0 <= v <= 0xFFFFFFFF","tryCatchPattern":null,"preventionTips":["Unpack network words as unsigned ('<I' not '<i')","Mask with & 0xFFFFFFFF when wrapping is intended","Range-check hash/CRC outputs before treating them as IPv4 integers"],"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"}