Skip to content

Instantly share code, notes, and snippets.

@jppommet
Last active November 5, 2024 08:56
Show Gist options
  • 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;
}
@ashtakshara
Copy link

ashtakshara commented May 10, 2021

Both work fine, thanks.

@cstephe
Copy link

cstephe commented Jun 17, 2021

Hi,

I've used both of your function, which converted "original_int -1062731546" to "converted ip 192.168.0.230"
Then I tried to reverse but it gave me "converted int 3232235750" instead of "-1062731546",

Here is the link to demo: https://stackblitz.com/edit/js-1ccktx

These methods don't work right, -1062731546 is correct, I'm just digging into this now, so I am not an expert, but anything over 128.0.0.0 goes into the negative space for 32 bit and would set the negative bit. or something like that.

@Marc-Klein
Copy link

Marc-Klein commented Mar 16, 2022

For the ip2int thing, you can also use

ip.split`.`.reduce((int, value) => int * 256 + +value)

@RuthiCohen
Copy link

Can you write ip2int function for ipv6 addresses please?

@eelcocramer
Copy link

eelcocramer commented Aug 31, 2022

For IPv6 you can use

ipv6.split(':').map(str => Number('0x'+str)).reduce(function(int, value) { return BigInt(int) * BigInt(65536) + BigInt(+value) })

updated code to use BigInt because 128 bit ipv6 address does not fit into a javascript number

@filecage
Copy link

filecage commented Mar 1, 2023

Hi,
I've used both of your function, which converted "original_int -1062731546" to "converted ip 192.168.0.230"
Then I tried to reverse but it gave me "converted int 3232235750" instead of "-1062731546",
Here is the link to demo: https://stackblitz.com/edit/js-1ccktx

These methods don't work right, -1062731546 is correct, I'm just digging into this now, so I am not an expert, but anything over 128.0.0.0 goes into the negative space for 32 bit and would set the negative bit. or something like that.

The methods do work correctly. -1062731546 as well as 3232235750 are a correct integer representation of the IP address 192.168.0.230. In binary, they're the exact same, but depending on whether you look at the value as signed integer (then the first bit means "negative value" and the remaining 31 bits mean 1062731546) or unsigned (then the 32 bits mean 3232235750).

>>> 0 in the ip2int function ensures a conversion to unsigned int. Removing it turns the result into the negative value you were looking for:

// 3232235750
'192.168.0.230'.split('.').reduce(function(ipInt, octet) { return (ipInt<<8) + parseInt(octet, 10)}, 0) >>> 0; 

// -1062731546
'192.168.0.230'.split('.').reduce(function(ipInt, octet) { return (ipInt<<8) + parseInt(octet, 10)}, 0);  

@elovin
Copy link

elovin commented Dec 20, 2023

@filecage

Would it not be shorter then to to replace <<8 with * 256 ?
It seems like the >>> 0 is then obsolete.

'192.168.0.230'.split('.').reduce(function(ipInt, octet) { return (ipInt * 256) + parseInt(octet, 10)}, 0); 
// 3232235750

@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