Skip to content

Instantly share code, notes, and snippets.

@tsaniel
Forked from 140bytes/LICENSE.txt
Created January 7, 2012 16:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsaniel/1575199 to your computer and use it in GitHub Desktop.
Save tsaniel/1575199 to your computer and use it in GitHub Desktop.
PHPJS - ip2long

ip2long

Converts a string containing an (IPv4) Internet Protocol dotted address into a proper address.

The function works with the following notation:

  1. Dot-decimal notation (192.0.2.235)
  2. Dotted Hexadecimal (0xC0.0x00.0x02.0xEB)
  3. Dotted Octal (0300.0000.0002.0353)
  4. Multiple notation (192.0x00.0002.0353)

The function will try to parse any inputs, so a non-complete address (10.0.0) also works; it will return NaN if the input is an invalid address.

long2ip: https://gist.github.com/1639378

See also: http://php.net/manual/en/function.ip2long.php

function(
a, // input for a (in)valid or non-complete IPv4 dotted address
b, // placeholder for the octet index
c, // placeholder for the result of sum expressed in decimal
d // placeholder for the current octet
) {
for (
c = b = 0; // initialize the octet index and the result
d = a.split('.')[b++]; // update the current octet, iterate if havn't handled all octets
c +=
d >> 8 // if the current octet is not within the range of 0~255
| // or
b > 4 ? // the octet index is greater than 4
NaN // make the result NaN
: // else
d * (1 << -8 * b) // calculate the number
)
d = parseInt( // convert the current octet into decimal according to the prefix
+d // parseInt omits extra words behind the first number, while + does not
&& // for example, parseInt regards '123abc' as 123, while + regards it as NaN
d // so we use + to check if the octet is valid first
);
return c
}
function(a,b,c,d){for(c=b=0;d=a.split('.')[b++];c+=d>>8|b>4?NaN:d*(1<<-8*b))d=parseInt(+d&&d);return c}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "ip2long",
"description": "Converts a string containing an (IPv4) Internet Protocol dotted address into a proper address.",
"keywords": [
"ip",
"phpjs",
"ip2long",
"long2ip",
"octet"
]
}
<!DOCTYPE html>
<title>Foo</title>
<div>Expected value: <b>3221226219,3221226219,3221226219,3221226219,167772160,NaN,NaN,NaN,NaN</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
// write a small example that shows off the API for your example
// and tests it in one fell swoop.
var myFunction = function(a,b,c,d){for(c=b=0;d=a.split('.')[b++];c+=d>>8|b>4?NaN:d*(1<<-8*b))d=parseInt(+d&&d);return c}
document.getElementById( "ret" ).innerHTML =
[myFunction('192.0.2.235'),
myFunction('0xC0.0x00.0x02.0xEB'),
myFunction('0300.0000.0002.0353'),
myFunction('192.0x00.0002.0353'),
myFunction('10.0.0'),
myFunction('10.-1.0.0'),
myFunction('10.0.0.0.1'),
myFunction('255.255.255.256'),
myFunction('127.0A.0.1'),
]
</script>
@subzey
Copy link

subzey commented Jan 19, 2012

Very nice -256 & d trick, bravo, @tsaniel!

Few more tricks can make this code 6 bytes shorter:
function(a,b,c,d){for(c=b=0;d=a.split('.')[b++];)c+=-256&d|b>4?NaN:d*(1<<-8*b);return c}

@tsaniel
Copy link
Author

tsaniel commented Jan 20, 2012

Genius! I didn't know the bitwise shift operator accepts negative numbers before.
By using this trick, we are able to put the increment of the index outside, thus there will be no infinitive loop any more, so b= is not needed.
Awesome!, @subzey!

By the way, I use parseInt to convert numbers so octal numbers are supported now.

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