IP Address Converter
Convert between IPv4, IPv6, decimal, binary, and hex formats. Shows address class and type.
Frequently Asked Questions
Code Implementation
import socket
import struct
def ip_to_int(ip: str) -> int:
return struct.unpack("!I", socket.inet_aton(ip))[0]
def int_to_ip(n: int) -> str:
return socket.inet_ntoa(struct.pack("!I", n))
def ip_to_hex(ip: str) -> str:
return "0x{:08X}".format(ip_to_int(ip))
def ip_to_binary(ip: str) -> str:
return ".".join(f"{int(o):08b}" for o in ip.split("."))
ip = "192.168.1.1"
print("Decimal:", ip)
print("Integer:", ip_to_int(ip)) # 3232235777
print("Hex: ", ip_to_hex(ip)) # 0xC0A80101
print("Binary: ", ip_to_binary(ip)) # 11000000.10101000.00000001.00000001
print("Back: ", int_to_ip(3232235777))Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.