Created
October 31, 2012 09:48
-
-
Save yulanggong/3986143 to your computer and use it in GitHub Desktop.
IP 和数字互相转换
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ip2number(ip){ | |
return parseInt(ip.replace(/(\d+)\.?/g, function(a,b){ | |
b = parseInt(b).toString(16); | |
return b.length === 1 ? '0' + b : b; | |
}), 16); | |
} | |
function number2ip(number){ | |
var ip = number & 255; | |
for (var i = 0; i < 3; i++){ | |
number >>= 8; | |
ip = (number & 255) + '.' + ip; | |
} | |
return ip; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment