def dec_to_bin(ip):
ip_array = ip.split(".")
ip_array = filter(None, ip_array)
if len(ip_array) != 4:
return "Invalid IP Address format"
else:
ip_bin = []
for x in range(len(ip_array)):
# Formatting example referenced from:
# http://stackoverflow.com/a/10411108/1170681
ip_bin.append('{0:08b}'.format(int(ip_array[x])))
ip_bin.append(".")
ip_bin.pop()
return ''.join(ip_bin)
This is a simple parser that will take a IP address in decimal form and convert it into its binary representation.
I'm looking for any tips on coding styles and improving the efficiency of the code.
split('.')
andappend(".")
for addresses that use:
instead of.
? – luiscubal Nov 9 '13 at 14:54::1
.split('.') (a valid IPv6 address) returns['::1']
. The len is 1, not 4, so it returns "Invalid IP Address format". – luiscubal Nov 9 '13 at 22:28