chevron_left chevron_right
Login Register invert_colors photo_library


Stay updated and chat with others! - Join the Discord!
Thread Rating:
  • 1 Vote(s) - 4 Average


Converting an IP to Decimal (Python 3.3) filter_list
Author
Message
Converting an IP to Decimal (Python 3.3) #1
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
[Image: CDUAq9d.png]

Reply

RE: Converting an IP to Decimal (Python 3.3) #2
(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:

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

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 version
Code:
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));

Reply

RE: Converting an IP to Decimal (Python 3.3) #3
(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.

Spoiler: Shortened version
Code:
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));

What does the ,0 do at the end of the first line?
[Image: CDUAq9d.png]

Reply

RE: Converting an IP to Decimal (Python 3.3) #4
(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.

Reply

RE: Converting an IP to Decimal (Python 3.3) #5
lol my god python has one unusual syntax
[Image: Z9DvuyJ.png]

Reply

RE: Converting an IP to Decimal (Python 3.3) #6
(05-13-2014, 01:52 AM)Duubz Wrote: 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.

Heh, I was confused because I didn't notice the x,total at the beginning (thought it was x). Thanks for the help!
[Image: CDUAq9d.png]

Reply

RE: Converting an IP to Decimal (Python 3.3) #7
(05-13-2014, 04:57 AM)xornull Wrote: lol my god python has one unusual syntax

That's why perl is better Tongue
XMPP - wrath@xmpp.jp

Reply

RE: Converting an IP to Decimal (Python 3.3) #8
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.
micrictor@creep.im

Reply

RE: Converting an IP to Decimal (Python 3.3) #9
(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.
[Image: CDUAq9d.png]

Reply

RE: Converting an IP to Decimal (Python 3.3) #10
(05-13-2014, 01:58 PM)Crypt Wrote: That's why perl is better Tongue

That's why we need a fucking perl section here but @Oni isn't responding
Unleash the lead from my pistol into my head bumpin' crystal

Reply






Users browsing this thread: 1 Guest(s)