Skip to content

Instantly share code, notes, and snippets.

@andfoy
Created November 14, 2014 02:20
Show Gist options
  • Save andfoy/673dd0d4806ad6c998b5 to your computer and use it in GitHub Desktop.
Save andfoy/673dd0d4806ad6c998b5 to your computer and use it in GitHub Desktop.
def ip2bin(ip):
num = [int(n) for n in ip.split('.')]
bits = ['0'*(8-len(bin(n)[2:]))+bin(n)[2:] for n in num]
return ''.join(bits)
def bin2ip(bin):
ip = [str(int(bin[i:i+8], 2))+'.' for i in range(0, 32, 8)]
return ''.join(ip)[0:-1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment