Skip to content

Instantly share code, notes, and snippets.

@jppommet
Last active December 26, 2023 13:44
Show Gist options
  • Star 63 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save jppommet/5708697 to your computer and use it in GitHub Desktop.
Save jppommet/5708697 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;
}
@filecage
Copy link

@elovin indeed, it is!

My example was to explain the difference between the two methods because someone was confused with the negative value being incorrect, which I demonstrated is not true. :)

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