Skip to content

Instantly share code, notes, and snippets.

@yulanggong
Created October 31, 2012 09:48
Show Gist options
  • Save yulanggong/3986143 to your computer and use it in GitHub Desktop.
Save yulanggong/3986143 to your computer and use it in GitHub Desktop.
IP 和数字互相转换
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