Skip to content

Instantly share code, notes, and snippets.

@mika76
Forked from jppommet/int2ip.js
Created March 22, 2020 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mika76/dc770d84bf934f0345e63ca83e298d0a to your computer and use it in GitHub Desktop.
Save mika76/dc770d84bf934f0345e63ca83e298d0a to your computer and use it in GitHub Desktop.
javascript conversion from ip address to long integer and vice versa
function int2ip (ipInt) {
return ( (ipInt>>>24) +'.' + (ipInt>>16 & 255) +'.' + (ipInt>>8 & 255) +'.' + (ipInt & 255) );
}
function ip2int(ip) {
return ip.split('.').reduce(function(ipInt, octet) { return (ipInt<<8) + parseInt(octet, 10)}, 0) >>> 0;
}
@mika76
Copy link
Author

mika76 commented Mar 22, 2020

Converting from C# seems to be inverted

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment