Skip to content

Instantly share code, notes, and snippets.

@st98
Last active August 29, 2015 14:13
Show Gist options
  • Save st98/720650082ddbac6edfb8 to your computer and use it in GitHub Desktop.
Save st98/720650082ddbac6edfb8 to your computer and use it in GitHub Desktop.
IP アドレスがなんか、こう。
// f(2130706433) === '127.0.0.1';
function f(n) {
return [
(n & 0xff << 24) >> 24,
(n & 0xff << 16) >> 16,
(n & 0xff << 8) >> 8,
n & 0xff
].map(String).join('.');
}
// g('127.0.0.1') === 2130706433;
function g(s) {
return s.match(/(?:\d{1,3}\.){3}\d{1,3}/)[0].split('.')
.reduce((c, p, i) => p |= (parseInt(c, 10) << 8 * (3 - i)), 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment