Skip to content
DeepForgeby svx
All problems
EasyAlgorithmsal-262

IPv4 Address to Integer

Problem

Convert a dotted IPv4 address string to its 32-bit unsigned integer value.

Treat the four octets as base-256 digits: multiply the running value by 256 and add each octet in order. Assume the address is well formed.

Hint

192.168.1.1 is 192 * 256 cubed plus 168 * 256 squared and so on.

Test cases

case 1
in: ["0.0.0.0"]
expected: 0
case 2
in: ["255.255.255.255"]
expected: 4294967295
case 3
in: ["192.168.1.1"]
expected: 3232235777
case 4
in: ["1.2.3.4"]
expected: 16909060
case 5
in: ["10.0.0.1"]
expected: 167772161
Starter codePython
def ip_to_int(ip):
    # Your code here
    pass

5 test cases · run your code against them in the editor