-
-
Save aachyee/91ee15a57e9f1187b707a4ff39dce4bb to your computer and use it in GitHub Desktop.
converts IP address to decimal 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
| /* IP2Decimal.js - converts IP address to decimal format | |
| * @author - Livingston Samuel | |
| */ | |
| var IP2Decimal = function (ip) { | |
| var ipSyntax = /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/, ipArr, i = 4, decVal = 0; | |
| if (ipSyntax.test(ip)) { | |
| ipArr = ip.split("."); | |
| while (i--) { | |
| if (ipArr[i] > 255) { | |
| throw new Error("Invalid IP Address"); | |
| } else { | |
| decVal += Math.pow(2, (8 * i)) * ipArr[3 - i]; | |
| } | |
| } | |
| return decVal; | |
| } 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