Skip to content

Instantly share code, notes, and snippets.

@monkeym4ster
Created March 18, 2016 07:04
Show Gist options
  • Save monkeym4ster/7eff5843863f2b373a1e to your computer and use it in GitHub Desktop.
Save monkeym4ster/7eff5843863f2b373a1e to your computer and use it in GitHub Desktop.
JavaScript IP to long
var ip;
ip = {};
ip.toLong = function toInt(ip){
var ipl=0;
ip.split('.').forEach(function( octet ) {
ipl<<=8;
ipl+=parseInt(octet);
});
return(ipl >>>0);
};
ip.fromLong = function fromInt(ipl){
return ( (ipl>>>24) +'.' +
(ipl>>16 & 255) +'.' +
(ipl>>8 & 255) +'.' +
(ipl & 255) );
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment