Wednesday, August 08, 2007

IP Address conversion

This took me way too long to figure out. Converts IP addresses from decimal notation to quad dotted form. In ASP.

Function MyHex(ByVal Number)
Dim Sign
Const HexChars = "0123456789ABCDEF"
Sign = Sgn(Number)
Number = Fix(Abs(CDbl(number)))
If Number = 0 Then
MyHex = "0"
Exit Function
End If
While Number > 0
MyHex = Mid(HexChars, 1 + (Number - 16 * Fix(Number / 16)), 1) & MyHex
Number = Fix(Number/16)
WEnd
If Sign = -1 Then MyHex = "-" & MyHex
End Function

Function decToQuad(Byval dec)

HexVal = MyHex(dec)
ip = ""

for i = 0 to 3
temp = Left(HexVal,2)
ip = ip & Eval("&H" & temp) & "."
Hexval = Right(Hexval, Len(Hexval) - 2)
next
decToQuad = Left(ip, Len(ip) - 1)
end function

No comments: