Converting IP Addresses in JavaScript

There are actually three different formats in which IP addresses can be displayed.The dotted version that people are most used to seeing has four numbers each between 0 and 256 with dots separating them. This is not what computers actually use as an IP address since to the computer an IP address is just a number. The dotted format that we are used to seeing is just one of the ways in which we attempt to convert that number the computer uses into a format that we can more easily understand and remember.

Let's consider an example. Probably the most commonly used IP address because it is the one that refers to the local computer is 2130706433 although you have probably come across it as 127.0.0.1. The third variant of the IP address that exists is the hexadecimal equivalent of 2130706433 which is 7f000001.

At this point if you know something of how hexadecimal numbers work you might have just realised how we can get from 2130706433 to 127.0.0.1 since if you take the 7f000001 (which is the hexadecimal equivalent of 2130706433) and split it into two digit groups and then convert each of those back to a decimal number and separate the resulting numbers by dots then you have 127.0.0.1 as the result.

The dotted notation that we have for IP addresses is worked out exactly that way. The number of the IP address that the computer uses (which it actually has stored in binary so that it uses 32 bits to hold the entire number) is split into four pieces and each of those as a decimal number is what we use in the dot notation that we regularly use for IP addresses.

So what has this to do with JavaScript? Well one thing we can do really easily with JavaScript is to convert IP addresses between these three formats (and that is in fact how I got the actual numbers for the address in the example above since JavaScript can perform these conversions much quicker than I can manally). So what do we need in the way of JavaScript to convert between the three formats? Well in the case of converting between the decimal and hexadecimal forms we can just use the simple d2h() and h2d() functions that are the easiest way to convert between decimal and hex regardless of what the numbers actually represent.

To convert between the dot notation and the decimal number is just a matter of multiplying by 256 going one way and dividing by 256 and taking remainders to go the other. The following two functions will do that for us.

function dot2num(dot) {
var d = dot.split('.');
return ((((((+d[0])*256)+(+d[1]))*256)+(+d[2]))*256)+(+d[3]);}

function num2dot(num) {
var d = num%256;
for (var i = 3; i > 0; i--) { 
num = Math.floor(num/256);
d = num%256 + '.' + d;}
return d;}

That just leaves converting between the hexadecimal and dot notations and the easiest way to go that is to just convert to the decimal number first and then convert from that to the format you want.

So now for a practical example. Just put an IP address in either dot or decimal notation in the following field to have it converted to the other. (Note if you enter anything that isn't a valid IP address in one of those two formats then your input will not be converted.)

Reference: http://javascript.about.com/library/blipconvert.htm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值