Use PHP functions in JavaScript

JavaScript long2ip

Converts an (IPv4) Internet network address into a string in Internet standard dotted format

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
20
function long2ip ( proper_address ) {
    // Converts an (IPv4) Internet network address into a string in Internet standard dotted format  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/long2ip    // +   original by: Waldo Malqui Silva
    // *     example 1: long2ip( 3221234342 );
    // *     returns 1: '192.0.34.166'
    
    var output = false;    
    if ( !isNaN( proper_address ) && ( proper_address >= 0 || proper_address <= 4294967295 ) ) {
        output = Math.floor(proper_address / Math.pow( 256, 3 ) ) + '.' +
            Math.floor( ( proper_address % Math.pow( 256, 3 ) ) / Math.pow( 256, 2 ) ) + '.' +
            Math.floor( ( ( proper_address % Math.pow( 256, 3 ) )  % Math.pow( 256, 2 ) ) / Math.pow( 256, 1 ) ) + '.' +            Math.floor( ( ( ( proper_address % Math.pow( 256, 3 ) ) % Math.pow( 256, 2 ) ) % Math.pow( 256, 1 ) ) / Math.pow( 256, 0 ) );
    }
    
    return output;
}
external links: original PHP docs | raw js source

Examples

Running

1
long2ip( 3221234342 );

Should return

1
'192.0.34.166'

Dependencies

No dependencies, you can use this function standalone.

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.


Authors

Thanks to the following developers, you get to have long2ip goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Kevin van Zonneveld
4 Sep '09 Permalink

q  @ John: That will happen if you try to move around IPs in long notation between systems anyway, cause the max integer size isn't fixed:

On 64 bits system ip2long ONLY RETURNS POSITIVE VALUES

so

echo ip2long('200.200.200.200');
?>

will output -926365496 on a 32 bits system and 3368601800 on a 64 bits system

So basically only use this in the confinement of 1 process, or use a methods to get consistent numbers like sprintf('%u') before transporting to database/client/whatever

Gravatar
John
30 Aug '09 Permalink

q  Cool idea. But this doesn't work. If you use ip2long in php, and get the result of that and pass it to this long2ip javascript function, what you get is NOT the original IP you started with. If you add (256^4)/2 to the long, you'll get the 2nd, 3rd and 4th sections correct but the first section of the IP will still be off.

long2ip/php accepts the signed int, but apparently the javascript version is looking for an unsigned int

Gravatar
Will
24 Apr '08 Permalink

q  Great job!
Very happy to see that is your recently post.
I am lucky, because of you!


Contribute a New function

More functions

In this category

inet_ntop
inet_pton
ip2long
» long2ip
setcookie
setrawcookie

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !