IP Address Programming Samples (Go, Python, JS, PHP, C)
Core patterns — validate, test CIDR membership, convert to integer — in several languages.
Go
ip := net.ParseIP("192.168.0.10")
_, cidr, _ := net.ParseCIDR("192.168.0.0/24")
fmt.Println(cidr.Contains(ip)) // true
Python
import ipaddress
ip = ipaddress.ip_address("192.168.0.10")
net = ipaddress.ip_network("192.168.0.0/24")
print(ip in net) # True
print(int(ip)) # 3232235530
JavaScript (Node.js)
import net from "node:net";
console.log(net.isIP("192.168.0.10")); // 4
const toInt = ip => ip.split(".").reduce((a,o)=>(a<<8>>>0)+ +o,0)>>>0;
console.log(toInt("192.168.0.10")); // 3232235530
PHP
var_dump(filter_var("192.168.0.10", FILTER_VALIDATE_IP) !== false);
echo ip2long("192.168.0.10"); // 3232235530
C
struct in_addr a;
inet_pton(AF_INET, "192.168.0.10", &a);
printf("%08x\n", a.s_addr);
For quick checks, try the Subnet calculator and IP converter.