Skip to content

Instantly share code, notes, and snippets.

@rishabhmhjn
Created July 1, 2016 04:52
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 rishabhmhjn/e6d01cc48ae38fdaaf597da2a023b87b to your computer and use it in GitHub Desktop.
Save rishabhmhjn/e6d01cc48ae38fdaaf597da2a023b87b to your computer and use it in GitHub Desktop.
Convert IP into Integer and Integer into IP
import _ from 'lodash';
import {
reduce
} from 'lodash';
const IP_CAL = [Math.pow(256, 3), Math.pow(256, 2), Math.pow(256, 1), Math.pow(256, 0)];
function ipToNum(ip) {
return reduce(ip.split('.'), (ipnum, o, i) => (ipnum += (Number(o) * (IP_CAL[i]))), 0);
}
function numToIp(ipnum) {
return reduce(IP_CAL, (o, d, i) => {
o.push(Math.floor(ipnum / d) % 256);
return o;
}, [])
.join('.');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment