Python Forum
Self Paced Web course - split ip and print each piece in binary and hex
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Self Paced Web course - split ip and print each piece in binary and hex
#1
Hi folks Im taking a free self paced web class .. the following code meets the exercise requirement.

ip = input("Enter an ip address : ")
octets = ip.split(".")
print("\n\n{:^15}{:^15}{:^15}{:^15}".format("Octet1","Octet2","Octet3","Octet4"))
print("-" * 80)
print("{:^15}{:^15}{:^15}{:^15}".format(*octets))
boct1 = bin(int(octets[0]))
boct2 = bin(int(octets[1]))
boct3 = bin(int(octets[2]))
boct4 = bin(int(octets[3]))
print("{:^15}{:^15}{:^15}{:^15}".format(boct1,boct2,boct3,boct4))
xoct1 = hex(int(octets[0]))
xoct2 = hex(int(octets[1]))
xoct3 = hex(int(octets[2]))
xoct4 = hex(int(octets[3]))
print("{:^15}{:^15}{:^15}{:^15}".format(xoct1,xoct2,xoct3,xoct4))
print("-" * 80)
I dont like whats going on on lines 6-9 and 11-14. Seems cheap to me.
I want to loop through the list, look at each octet once .. convert to a bin value and hex and assign to respective vars to be printed later in the script. I added a for loop shown below. The code is not working.

My question is .. How do I setup the loop to create vars
boct1 - var containing the binary equivalent of octet one
hoct1 - var containing the hex equivalent of octet one

boct2 - var containing the binary equivalent of octet two
hoct2 - var containing the hex equivalent of octet two
etc etc

My attempt at the loop is as follows :
ip = input("Enter an ip address : ")
octets = ip.split(".")
#
for i in range(len(octets)):
     print(octets[i])
     boct[i] = bin(int(octets[i]))
     xoct[i] = hex(int(octets[i]))
#
print("\n\n{:^15}{:^15}{:^15}{:^15}".format("Octet1","Octet2","Octet3","Octet4"))
print("-" * 80)
print("{:^15}{:^15}{:^15}{:^15}".format(*octets))
boct1 = bin(int(octets[0]))
boct2 = bin(int(octets[1]))
boct3 = bin(int(octets[2]))
boct4 = bin(int(octets[3]))
print("{:^15}{:^15}{:^15}{:^15}".format(boct1,boct2,boct3,boct4))
xoct1 = hex(int(octets[0]))
xoct2 = hex(int(octets[1]))
xoct3 = hex(int(octets[2]))
xoct4 = hex(int(octets[3]))
print("{:^15}{:^15}{:^15}{:^15}".format(xoct1,xoct2,xoct3,xoct4))
print("-" * 80)
Thanks folks
Sum

... geez .. soon as I send these .. I get it ..

just append each to a binarylist and hexlist respectively .. duhhh !!
Reply
#2
You should post outputs and/or errors to help people understand the issues that you are having.

You need to create the lists first, then append the values:
octets = ip.split(".")
boct = []
xoct = []
for octet in octets:
    boct.append(bin(int(octet)))
    xoct.append(hex(int(octet)))
print("\n\n{:^15}{:^15}{:^15}{:^15}".format("Octet1","Octet2","Octet3","Octet4"))
print("-" * 80)
print("{:^15}{:^15}{:^15}{:^15}".format(*octets))
print("{:^15}{:^15}{:^15}{:^15}".format(boct[0],boct[1],boct[2],boct[3]))
print("{:^15}{:^15}{:^15}{:^15}".format(xoct[0],xoct[1],xoct[2],xoct[3]))
print("-" * 80)
Reply
#3
Bad question. Would avoid boct and xoct completely.
ip = input("Enter an ip address : ")
octets = ip.split(".")
print("\n\n{:^15}{:^15}{:^15}{:^15}".format("Octet1","Octet2","Octet3","Octet4"))
print("-" * 80)
print("{:^15}{:^15}{:^15}{:^15}".format(*octets))
for x in range(4):
    print("{:^15}".format(bin(int(octets[x]))), end="")
print()
for x in range(4):
    print("{:^15}".format(hex(int(octets[x]))), end="")
print()
print("-" * 80)
You can get fancier with list comprehension
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  name.split() + (And) + print question sklord 12 3,713 Mar-26-2022, 05:45 PM
Last Post: deanhystad
  Explain a piece of code SuuzyQ04 1 1,572 Nov-19-2020, 07:13 PM
Last Post: Gribouillis
  Binary to decimal, print the decimal rs74 3 2,066 Jul-12-2020, 05:25 PM
Last Post: DPaul
  Print Binary Tree - Python Daniel94 1 2,658 Jan-08-2020, 09:54 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020