Instantly share code, notes, and snippets.

Created January 7, 2012 16:29
Show Gist options
• 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)
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

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 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 }
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 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}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004 Copyright (C) 2011 YOUR_NAME_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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 { "name": "ip2long", "description": "Converts a string containing an (IPv4) Internet Protocol dotted address into a proper address.", "keywords": [ "ip", "phpjs", "ip2long", "long2ip", "octet" ] }
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 Foo
Expected value: 3221226219,3221226219,3221226219,3221226219,167772160,NaN,NaN,NaN,NaN
Actual value:

### 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 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.