Convert Hexadecimal to Dotted IPv4
Problem statement
Convert a 32-bit IPv4 address written as exactly eight hexadecimal digits into dotted-decimal notation. An optional leading 0x or 0X is allowed and hex letters are case-insensitive.
Interpret the first byte as the first decimal octet. Return INVALID for any other length or character.
Function
hexToIpv4(value: String) → StringExamples
Example 1
value = "C0A80101"return = "192.168.1.1"The four bytes C0, A8, 01, and 01 become the four octets.
Example 2
value = "0x7F000001"return = "127.0.0.1"The optional prefix is removed before byte conversion.
Example 3
value = "ABC"return = "INVALID"The payload must contain exactly eight hexadecimal digits.
Constraints
0 <= value.length <= 20.- The result must use ordinary decimal octets without leading zeros.