Skip to content

Instantly share code, notes, and snippets.

@A1rPun
Last active August 29, 2017 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save A1rPun/e661fbae4a2baef0aa963f09caf13763 to your computer and use it in GitHub Desktop.
Save A1rPun/e661fbae4a2baef0aa963f09caf13763 to your computer and use it in GitHub Desktop.
int32 to IP in JavaScript
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