Created
July 1, 2016 04:52
-
-
Save rishabhmhjn/e6d01cc48ae38fdaaf597da2a023b87b to your computer and use it in GitHub Desktop.
Convert IP into Integer and Integer into IP
This file contains hidden or 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
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