Skip to content

Instantly share code, notes, and snippets.

@igrek8
Last active December 22, 2016 19:30
Show Gist options
  • Save igrek8/9b804afbe23e96ddba7bcd2af05ed7c1 to your computer and use it in GitHub Desktop.
Save igrek8/9b804afbe23e96ddba7bcd2af05ed7c1 to your computer and use it in GitHub Desktop.
ip2num, ipv4 to number, mac to number, number to mac, num2ip
function ipv42num(ipString: string) {
const ipNumber = ipString.split('.');
return ((((((+ipNumber[0]) * 256) + (+ipNumber[1])) * 256) + (+ipNumber[2])) * 256) + (+ipNumber[3]);
}
function num2ipv4(ipNumber: number) {
let ipString = (ipNumber % 256).toString();
for (let i = 3; i > 0; i--) {
ipNumber = Math.floor(ipNumber / 256);
ipString = ipNumber % 256 + '.' + ipString;
}
return ipString;
}
function mac2num(macNumber: number) {
return String(1e12 + (macNumber)
.toString(16)).slice(-12).match(/.{1,2}/g).join(':');
}
function num2mac(macString: string) {
return parseInt(macString.split(':').join(''), 16);
}
function calcAlloc(data: number) {
return Math.floor(Math.log2(data) / 4) + 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment