![]() |
|
Converting an IP to Decimal (Python 3.3) - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Python (https://sinister.ly/Forum-Python) +--- Thread: Converting an IP to Decimal (Python 3.3) (/Thread-Converting-an-IP-to-Decimal-Python-3-3) |
Converting an IP to Decimal (Python 3.3) - Shebang - 05-13-2014 Honestly, I don't know if I'm using the best practices here for parsing the input, but here it is: Code: try:
x = list(map(int, input("Enter an IP Address: ").split(".")))
if len(x) == 4:
total = 0
for i in range(0, 4):
total += x[i] << (8 * (3 - i))
print("IP to decimal: " + str(total))
else:
print("Invalid IP address.")
except ValueError:
print("Invalid IP Address.")This just converts an IP address to it's decimal counterpart (which can be interpreted by things like Chrome), knowing the following pattern: IP Address: 10.9.8.7 Decimal = (10 << 24) + (9 << 16) + (8 << 8) + 7 = (10 * 2^24) + (9 * 2^16) + (8 * 2^8) + 7 = 168364039 RE: Converting an IP to Decimal (Python 3.3) - Equinox - 05-13-2014 (05-13-2014, 01:02 AM).Shebang Wrote: Honestly, I don't know if I'm using the best practices here for parsing the input, but here it is: You did a pretty good job. I won't say much is wrong with it, but I will say that it can be shortened. Spoiler: Shortened versionCode: x,total = list(map(int, input('IP: ').split('.'))),0;
if len(x) == 4:
for i in range(0,4): total += x[i] << (8 * (3 - i)); print(str(total));RE: Converting an IP to Decimal (Python 3.3) - Shebang - 05-13-2014 (05-13-2014, 01:36 AM)Duubz Wrote: You did a pretty good job. I won't say much is wrong with it, but I will say that it can be shortened. What does the ,0 do at the end of the first line? RE: Converting an IP to Decimal (Python 3.3) - Equinox - 05-13-2014 (05-13-2014, 01:46 AM).Shebang Wrote: What does the ,0 do at the end of the first line? Variables. We can split them with commas. user1,user2 = 'duubz','.shebang' You could think of it like a dictionary. {user1: 'duubz', user2: '.shebang'} It's basically the same as assigning the variables separately. user1 = 'duubz user2 = '.shebang' Basically just saves space. RE: Converting an IP to Decimal (Python 3.3) - xornull - 05-13-2014 lol my god python has one unusual syntax RE: Converting an IP to Decimal (Python 3.3) - Shebang - 05-13-2014 (05-13-2014, 01:52 AM)Duubz Wrote: Variables. We can split them with commas. Heh, I was confused because I didn't notice the x,total at the beginning (thought it was x). Thanks for the help! RE: Converting an IP to Decimal (Python 3.3) - Crypt - 05-13-2014 (05-13-2014, 04:57 AM)xornull Wrote: lol my god python has one unusual syntax That's why perl is better
RE: Converting an IP to Decimal (Python 3.3) - w00t - 05-13-2014 Why on earth are you doing multiple assignment when neither the variables nor their values are related? It uses more characters( albeit less lines ) and, as is evident, is much harder to read. RE: Converting an IP to Decimal (Python 3.3) - Shebang - 05-13-2014 (05-13-2014, 04:13 PM)w00t Wrote: Why on earth are you doing multiple assignment when neither the variables nor their values are related? It uses more characters( albeit less lines ) and, as is evident, is much harder to read. Are you talking to me or Duubz? I don't have multiple assignment in my original code. RE: Converting an IP to Decimal (Python 3.3) - Alan Turing - 05-13-2014 (05-13-2014, 01:58 PM)Crypt Wrote: That's why perl is better That's why we need a fucking perl section here but @Oni isn't responding |