Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def ip_to_decimal():
- # Ask the user for an IP address
- ip_address = input("Enter an IP address (e.g., 192.168.0.1): ").strip()
- # Split the IP address into its four parts
- parts = ip_address.split(".")
- if len(parts) != 4:
- print("Invalid IP address format.")
- return
- binary_str = ""
- for part in parts:
- try:
- # Convert each octet to an integer and validate its range
- num = int(part)
- if not (0 <= num <= 255):
- print("Each octet must be between 0 and 255.")
- return
- # Convert the integer to an 8-bit binary string and concatenate
- binary_str += format(num, '08b')
- except ValueError:
- print("Invalid input. Please enter numbers only.")
- return
- # Convert the 32-bit binary string to a decimal integer
- decimal_value = int(binary_str, 2)
- print("The decimal representation is:", decimal_value)
- if __name__ == "__main__":
- ip_to_decimal()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement