IPv4 Address to Integer
Problem Statement
Convert a dotted-quad IPv4 address string to its 32-bit integer value.
Background
Each of the four octets contributes 8 bits: the integer is a*2563 + b256**2 + c256 + d**, i.e. the big-endian packing of the four bytes.
Your Task
def ip_to_int(ip):
Return the integer value of the address.
Input Format
- ip (str): "a.b.c.d" with each octet 0..255.
Output Format
- A single int.
Sample
print(ip_to_int("192.168.1.1"))
Output:
3232235777
Example
print(ip_to_int("192.168.1.1"))3232235777
- Split the input string
"192.168.1.1"by dots to extract the four octets: , , , . - Initialize the accumulator to and process the first octet (): .
- Process the second octet () by shifting the previous value left by 8 bits (multiplying by 256) and adding the new byte: .
- Process the third octet (): .
- Process the fourth octet (): .
- The final output is 3232235777
Constraints
- Four octets, each 0..255.
- Big-endian: leftmost octet is most significant.
- Return an int.