Skip to content

Instantly share code, notes, and snippets.

@livingston
Created January 12, 2010 16:24
Show Gist options
  • Save livingston/275329 to your computer and use it in GitHub Desktop.
Save livingston/275329 to your computer and use it in GitHub Desktop.
converts IP address to octal format
/* IP2Octal.js - converts IP address to Octal format
* @author - Livingston Samuel
*
* @requires - padString.js - http://gist.github.com/275259#file_pad_string.js
*/
var IP2Octal = function (ip) {
var ipSyntax = /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/, ipArr, i = 4, result = [];
if (ipSyntax.test(ip)) {
ipArr = ip.split(".");
while (i--) {
if (ipArr[i] > 255) {
throw new Error("Invalid IP Address");
} else {
result.push(padString(parseInt(ipArr[i], 10).toString(8), 0, 8));
}
}
return result.reverse().join(".");
} else {
throw new Error("Invalid IP Address");
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment