Last active
August 29, 2017 15:55
-
-
Save A1rPun/e661fbae4a2baef0aa963f09caf13763 to your computer and use it in GitHub Desktop.
int32 to IP in JavaScript
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 int32toIP(nMask) { | |
for (var nFlag = 0, nShifted = nMask, sMask = ''; nFlag < 32; | |
nFlag++, sMask += String(nShifted >>> 31), nShifted <<= 1); | |
return sMask.match(/.{1,8}/g).map(function (x) { return parseInt(x, 2); }).join('.'); | |
} | |
int32toIP(255); // "0.0.0.255" | |
int32toIP(256); // "0.0.1.0" | |
int32toIP(1333333337); // "79.121.13.89" | |
int32toIP(7527203073); // "192.168.1.1" | |
int32toIP(6425673729); // "127.0.0.1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment