Created
January 12, 2010 16:24
-
-
Save livingston/275329 to your computer and use it in GitHub Desktop.
converts IP address to octal format
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
/* 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