random script dump
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

base256.py 673B

123456789101112131415161718192021222324252627282930
  1. #!/usr/bin/env python
  2. """A very simple script to convert an integer into an IPv4 address.
  3. Expects an integer as the only argument."""
  4. from sys import argv
  5. def from_base256(number):
  6. num = int(number)
  7. addr = list()
  8. for e in range(3, -1, -1):
  9. multiplier = 256 ** e
  10. octet = num // multiplier
  11. addr.append(str(octet))
  12. num = num % multiplier
  13. return '.'.join(addr)
  14. def to_base256(addr):
  15. addr = addr.split('.')
  16. num = 0
  17. for e in range(3, -1, -1):
  18. num += int(addr[3-e]) * 256 ** e
  19. return str(num)
  20. try:
  21. result = from_base256(argv[1])
  22. except ValueError:
  23. result = to_base256(argv[1])
  24. print(result)