Recently, I was going through some code and found something strange (??? !!!) In short, In one place they were passing an IP address and on the other place they were using it as an integer.. I didn’t understand what is happening and then I searched the internet and found something that explained it..
For those who may not know, IP address is the identity of a device in a network. Like your postal address. And each IP is of the format X.X.X.X and each segment (indicated by X) is an octet. And the values of each octet can be from 0-255. Each IP address can be converted into a single number using the formula:
4th octet * (256 ^ 0) + 3rd octet * (256 ^ 1) + 2nd octet * (256 ^ 2) + 1st octet * (256 ^ 3)
This will give a number and that number is the actual address for a server/machine in network.
For ex. 126.96.36.199 is the IP of google.com and as per formula
(206 * 256 ^ 0 + 218 * 256 ^ 1 + 58 * 256 ^ 2 + 216 * 256 ^ 3 = 3627735758 )
Additional info: When you type google.com in the browser (or any other site’s address for that matter), the request will be sent to a server. That server will have the information saying google.com is this IP and send the request to that IP. These servers are called as DNS (Domain Name Servers) servers.