Advertisement
makispaiktis

Int32 to IP adrresses

Jun 15th, 2021 (edited)
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.75 KB | None | 0 0
  1. '''
  2. Take the following IPv4 address: 128.32.10.1
  3. This address has 4 octets where each octet is a single byte (or 8 bits).
  4. 1st octet 128 has the binary representation: 10000000
  5. 2nd octet 32 has the binary representation: 00100000
  6. 3rd octet 10 has the binary representation: 00001010
  7. 4th octet 1 has the binary representation: 00000001
  8. So 128.32.10.1 == 10000000.00100000.00001010.00000001
  9. Because the above IP address has 32 bits, we can represent it as the unsigned 32 bit number: 2149583361
  10. Complete the function that takes an unsigned 32 bit number and returns a string representation of its IPv4 address.
  11. '''
  12. from math import log2, log10
  13. from random import randrange
  14.  
  15. # Function 1 - Binary to decimal
  16. def binaryToDecimal(binary):
  17.     N = len(str(binary))
  18.     decimal = 0
  19.     for i in range(N):
  20.         decimal += int(str(binary)[i]) * (2 ** (N-1-i))
  21.     return str(decimal)                # Return string
  22.  
  23. # Function 2 - Decimal to binary
  24. def decimalToBinary(decimal):
  25.     if decimal == 0:
  26.         return "0"
  27.     N = int(log2(decimal)) + 1
  28.     binary = []
  29.     for i in range(N-1, -1, -1):
  30.         if decimal >= 2 ** i:
  31.             binary.append(str(1))
  32.             decimal -= 2 ** i
  33.         else:
  34.             binary.append(str(0))
  35.     return ''.join(binary)              # Return string
  36.  
  37.  
  38. # Function 3 - Int32 to IP
  39. def int32ToIP(int32):
  40.     if int32ToIP == 0:
  41.         return "0.0.0.0"
  42.     # int32 is 2149583361 for example ---> maybe its binary representation is not 32 bytes --->
  43.     # we have to fill up with 0's in the beginning of the binary word
  44.     binary = decimalToBinary(int32)
  45.     N = len(binary)
  46.     filledBinary = []
  47.     if len(binary) < 32:
  48.         # We have to fill up with zeros
  49.         filledBinary = [str(0) for i in range(32-N)]
  50.     # Now, my new variable is a string with 0s and 1s with length 32
  51.     filledBinary.extend(binary)
  52.     filledBinary = ''.join(filledBinary)
  53.     # Filled binary is a 32bit-string like this:  10000000001000000000101000000001
  54.     binary1 = filledBinary[0:8]
  55.     binary2 = filledBinary[8:16]
  56.     binary3 = filledBinary[16:24]
  57.     binary4 = filledBinary[24:]
  58.     decimal1 = binaryToDecimal(binary1)
  59.     decimal2 = binaryToDecimal(binary2)
  60.     decimal3 = binaryToDecimal(binary3)
  61.     decimal4 = binaryToDecimal(binary4)
  62.     print(int32)
  63.     return str(decimal1) + "." + str(decimal2) + "." +str(decimal3) + "." +str(decimal4)
  64.  
  65. # MAIN FUNCTION
  66. int32_0 = 2149583361
  67. print(int32ToIP(int32_0))
  68. print()
  69. int32_00 = 16
  70. print(int32ToIP(int32_00))
  71. print()
  72. int32_1 = randrange(2**32)
  73. print(int32ToIP(int32_1))
  74. print()
  75. int32_2 = randrange(2**32)
  76. print(int32ToIP(int32_2))
  77. print()
  78. int32_3 = randrange(2**32)
  79. print(int32ToIP(int32_3))
  80. print()
  81. int32_4 = randrange(2**32)
  82. print(int32ToIP(int32_4))
  83. print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement