Web Hosting Info

Search:

featured partner

The IP to Country Database

  Forum Topics : Development / IP Address to IP Number Formula
Submitted by andthereitgoes on Fri, 06/11/2004 - 20:28.
Hi
could someone please explain to me
the logic behind the IP Address to IP Number Formula.
eg:IP Number = A x (256*256*256) + B x (256*256) + C x 256 + D

why A is multiplied by 256^3
why B is multiplied by 256^2
why C is multiplied by 256^1
why D is multiplied by 256^0

Can any one pls explain..
Andthereitgoes
Comment viewing options:
Select your preferred way to display the comments and click 'Save settings' to submit your changes.
Simple Math
Posted by McAfee on Sat, 06/12/2004 - 06:52.
The formula is the same Math formula to convert a base-10 numbering system to any other system (like base 256). The base simple specifies how many possible values or symbols can be represented on one digit (or on this case, groups seperated by periods).

As you know, 1492 is actually
10^3 x 1 = 1000
10^2 x 4 = 400
10^1 x 9 = 90
10^0 x 2 = 2
And then you add it all up. You know the 4 is a 400 because it's on the hundreads digit. (decimal system, base-10)

Same for HEX numbers wich are base 16 (0 to F). But you must subtitute letters A to F with 10 to 15 respectivly.
Example: 5D4
16^2 x 5 = 1280
16^1 x 13 = 208 (d equals 13)
16^0 x 4 = 4
1492

dotted IP address are the same, but you are already have the value given to you, instead of a letter or symbol.
It's like if I wrote the hex above as 5.13.4 (Base-16)

By now you should know that dotted IP's are base 256. Each group can contain 256 values, from 0 to 255 (zero included). The same formula applies.

You can find a more cientific formula on the net, just look for number system conversion, or base conversion. You may also find the formula to reverse the process.
Another explanation
Posted by Daath on Mon, 07/12/2004 - 18:15.
An IP address consists of four bytes (each of eight bits), an unsigned long integer also consists of four bytes, it is thus feasible to translate a dotted quad (an IP number) back and forth between four bytes and an unsigned long integer (uint32).

What you do is add the first byte to an uint32, shift it eight bits to the left, then apply binary OR to the next byte against (or just add it to) the uint32 shift it again and do the same two more times. Now you have translated an IP to a long number.

To reverse it you could apply binary AND against 0xFF (256), to get the first byte, then shift the number 8 bits to the right, do the same three times, and you have your IP number again :)

-
Any technology distinguishable from magic, is insufficiently advanced.