ASP: IPv4 Address Manipulation
Introduction
Author: Martin WarningYear: 2007
Updated: 2008
License: Free
These are some functions that can be used to convert an IP address from dotted decimal to binary to decimal and from decimal to binary to dotted decimal. This can be useful when you need to compare IP addresses or to save space in storing the IP address in a database.
Code
Languages:- XHTML
- ASP
- VBScript
Function fncBinaryToDecimal(intBinaryValue) Do fncBinaryToDecimal = fncBinaryToDecimal + (Left(intBinaryValue, 1) * 2 ^ (Len(intBinaryValue) - 1)) intBinaryValue = Mid(intBinaryValue, 2) Loop Until intBinaryValue = "" End Function
Function to convert decimal value to binary
Function fncDecimalToBinary(intDecimalValue) Dim intRange : intRange = 4294967296 ' IP's maximum decimal value Do While intRange >= 1 If intDecimalValue >= intRange then intDecimalValue = intDecimalValue - intRange fncDecimalToBinary = fncDecimalToBinary & "1" Else fncDecimalToBinary = fncDecimalToBinary & "0" End if intRange = intRange / 2 Loop End Function
Function to convert IP to Binary
Function fncIPToBinary(strIP) Dim intNewIP : intNewIP = Split(strIP,".") Dim i For i = 0 To UBound(intNewIP) fncIPToBinary = fncIPToBinary & Right(fncDecimalToBinary(cInt(intNewIP(i))),8) Next End Function
To convert the IP address to a decimal value instead of the binary value you just need to send the value generated by the fncIPToBinary function to the fncBinaryToDecimal function.
Function to convert binary value to IP
Function fncBinaryToIP(intIP) Dim intNewIP(3),i intNewIP(0) = Left(intIP,8) intNewIP(1) = Right(Left(intIP,16),8) intNewIP(2) = Left(Right(intIP,16),8) intNewIP(3) = Right(intIP,8) For i=0 To 3 If i > 0 Then fncBinaryToIP = fncBinaryToIP & "." End If fncBinaryToIP = fncBinaryToIP & fncBinaryToDecimal(intNewIP(i)) Next End Function
The following validation can be added to make sure that the entered binary value is numeric and also create a binary value of exactly 32 bits if the start value is shorter than 32 bits.
'Validation If Len(Replace(Replace(intTestValue,1,""),0,"")) > 0 Then ' Make sure the binary value is a binary value intTestValue = "" ElseIf Len(intTestValue) < 32 AND intTestValue <> "" Then ' leading 0 when binary value isn't 32 bits For j=1 To (32 - Len(intTestValue)) intTestValue = "0" & intTestValue Next ElseIf Len(intTestValue) > 32 Then ' select first 32 bits if binary value is longer than 32 bits intTestValue = Left(intTestValue,32) End If
Example to convert decimal value to IP
fncBinaryToIP(fncDecimalToBinary(cInt(intTestValue)))
All you need is to use the previous shown fncDecimalToBinary, fncBinaryToIP and fncBinaryToDecimal functions to translate a decimal value to IP. Next is the validation of the decimal input, it has to be numeric and of a value between 0 and 4294967296. The validation goes before you call the translate functions.
'Validation If NOT isNumeric(intTestValue) Then intTestValue = "" ElseIf intTestValue > 4294967296 OR intTestValue < 0 Then intTestValue = "" End If
