How To Translate An Ip Address

Table of contents:

How To Translate An Ip Address
How To Translate An Ip Address

Video: How To Translate An Ip Address

Video: How To Translate An Ip Address
Video: Using nslookup to resolve domain names to ip addresses 2024, May
Anonim

An IP address is made up of four decimal numbers, each of which can range from 0 to 255. Each such number is equivalent to two-digit hexadecimal or eight-bit binary, and is therefore called an octet. These four short numbers sometimes need to be translated into one long when writing scripts.

How to translate an ip address
How to translate an ip address

Instructions

Step 1

Multiply the first octet of the IP address by 16777216, or, equivalently, 256 to the third power. For example, if we are talking about the IP address 192.168.1.1 (it is often found in small local networks), then after multiplying the number 192 by 16777216 you get 3221225472.

Step 2

Multiply the second number by 65536 - that's how much you get if you raise 256 to the second power. For example, in the address 192.168.1.1, you have to multiply 168 by 65536, and you get 11010048.

Step 3

Multiply the third octet by 256 to the first power - that is, by the number itself 256. If you translate the IP address 192.168.1.1 into long form, then the result of this multiplication will be 256 * 1 = 256.

Step 4

Leave the fourth number unchanged, which is equivalent to multiplying by one. This is due to the fact that if you raise the number 256 (or any other number) to the zero power, you get 1. In the IP address 192.168.1.1, the result of the multiplication will be 1 * 1 = 1.

Step 5

Add all four multiplication results together. For the example considered here, the amount will look like this: 3232235777.

Step 6

When programming in PHP, use the ready-made function ip2long to translate an IP address into a long form. A function similar in purpose can be composed and included in a program or a separate module in another programming language.

Step 7

The task of translating an IP address from a long form back to a short one occurs much less frequently. To do this, divide the long address by 16777216, and the whole part of the division result becomes the first octet. Divide the remainder (don't confuse it with the fractional part) by 65536 to get the second octet, and so on. On engineering calculators, calculate the modulus as follows: [C] first number [MOD] second number [=]. The simplest calculators do not have this feature.

Step 8

When writing a function for performing a reverse translation in a particular programming language, use functions for integer division and calculating the remainder of the division. For example, in Pascal they are called div and mod, respectively. A fragment of the program for carrying out such a translation may look like this:

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;

Recommended: