Convert a 32-bit hexadecimal address to dotted-decimal IPv4 notation.
Implement hex_to_ipv4(text: string) -> string.
Constraints & Assumptions
The source reports a hex-to-IPv4 conversion without exact formatting rules. This practice contract uses network-order bytes: the leftmost byte is the first decimal octet.
-
Input length is at most 100. Accept exactly eight hexadecimal digits, optionally preceded by
0x
or
0X
.
-
Hexadecimal digits are case-insensitive. Whitespace, signs, separators, and any other length or character are invalid.
-
On valid input return four decimal octets separated by dots, without leading zeroes. On invalid input return
INVALID
.
-
Treat the value as an unsigned 32-bit bit pattern; addresses whose high bit is set are valid.
Examples
hex_to_ipv4("C0A80101") -> "192.168.1.1"
hex_to_ipv4("0xffffffff") -> "255.255.255.255"
hex_to_ipv4("00000000") -> "0.0.0.0"
hex_to_ipv4("1FF") -> "INVALID"
Explain how your byte-order choice and signed-integer handling affect the result. A solution that parses four two-character byte values can avoid signed 32-bit overflow entirely.