How to translate the IP address

How to translate the IP address

The IP address consists of four decimal numbers, each of which can accept value from 0 to 255. Each such number is equivalent two-bit hexadecimal or eight-bit binary and therefore is called an octet. These four short numbers by drawing up scripts sometimes require transfer to one long.

Instruction

1. Increase the first octet of the IP address by number 16777216, or that the same, 256 in the third degree. For example, if it is about IP address 192.168.1.1 (it often meets in the small local area networks), then after multiplication of number 192 on 16777216 3221225472 will turn out.

2. Increase the second by 65536 - it will turn out so much if 256 build in the second degree. For example, in address 192.168.1.1 it is necessary to increase 168 by 65536, and 11010048 will turn out.

3. Increase the third octet by 256 in the first degree - that is, on number 256. If you transfer to long IP address 192.168.1.1 form, then the result of such multiplication will be equal to 256*1=256.

4. Leave the fourth without changes that is equivalent to multiplication by unit. It is connected with the fact that if to build number 256 (or any other number) in zero degree, then 1 will turn out. In the IP address 192.168.1.1 result of multiplication will be 1*1=1.

5. Put all four results of multiplication among themselves. For the example reviewed here the sum will look as follows: 3232235777.

6. When programming in the PHP language use the ready ip2long function for transfer of the IP address to a long form. Function similar to destination can be made and included in the program or the separate module in other programming language.

7. The problem of the translation of the IP address from a long form back in short arises much less often. That to make it, divide the long address into 16777216, and the integer part of result of division will become the first octet. Divide the rest (do not mix it with a fractional part) into 65536, and the second octet and so on will turn out. On engineering calculators carry out calculation of a remainder of division so: [C] the first [MOD] the second [=]. In the simplest calculators this function is absent.

8. By drawing up function for implementation of back translation in any given programming language use functions for integer division and calculation of a remainder of division. For example, in language Pascal they are called, respectively, div and mod. The fragment of the program for implementation of such translation can look so:

octet [1]: =longip div 16777216;
nextnumber: =longip mod 16777216;
octet [2]: =nextnumber div 65536;
nextnumber: =nextnumber mod 65536;
octet [3]: =nextnumber div 256;
octet [4]: =nextnumber mod 256;

Author: «MirrorInfo» Dream Team


Print