Need to Convert DEC number to HEX number to IP ADDRESS

cpremo

Registered User.
Local time
Today, 03:36
Joined
Jun 22, 2007
Messages
50
Does anyone have code that will convert a DEC number to and IP Address?
 
Thanks to Jason on the Novell NSure Audit forum (which is the program were the data came from), I used his function (slightly modified) to calculate the IP addresses. I used the following formula on forms and reports

=IIf([SourceField]<>0,fnIPConv(([SourceField])))

These formulas call the function below:

Public Function fnIPConv(iIPDecimal As Long)
' Convert NSure Audit stored IP to readable form
' Input is an IP address stored as decimal
' Output is an IP address formatted as dotted quad
Dim iOne As Integer
Dim iTwo As Integer
Dim iThree As Integer
Dim iFour As Integer
Dim iFive As Integer
Dim iSix As Integer
Dim iSeven As Integer
Dim iEight As Integer
Dim iTempIP As Long
Dim strReturn As String

' Extract each hex number, since VBA is missing a dec2hex function
iTempIP = iIPDecimal
iEight = iTempIP Mod 16
iTempIP = (iTempIP - iEight) / 16
iSeven = iTempIP Mod 16
iTempIP = (iTempIP - iSeven) / 16
iSix = iTempIP Mod 16
iTempIP = (iTempIP - iSix) / 16
iFive = iTempIP Mod 16
iTempIP = (iTempIP - iFive) / 16
iFour = iTempIP Mod 16
iTempIP = (iTempIP - iFour) / 16
iThree = iTempIP Mod 16
iTempIP = (iTempIP - iThree) / 16
iTwo = iTempIP Mod 16
iTempIP = (iTempIP - iTwo) / 16
iOne = iTempIP Mod 16
'The first part of the If statement converts negative values
'The second converts non-negative values (i.e., Loopback IPs)
If Left(iIPDecimal, 1) = "-" Then
strReturn = CStr(255 + (iOne * 16 + iTwo)) & "." & CStr(255 + (iThree * 16 + iFour)) & "." & CStr(255 + (iFive * 16 + iSix)) & "." & CStr(255 + (iSeven * 16 + iEight)) + 1
Else
strReturn = CStr(iOne * 16 + iTwo) & "." & CStr(iThree * 16 + iFour) & "." & CStr(iFive * 16 + iSix) & "." & CStr(iSeven * 16 + iEight)
End If
fnIPConv = strReturn

End Function
 
Just for the record, my VBA has a Hex() function.
 
I love the passive aggressiveness. ;) (Don't mention Dec() or Oct()! ;P.)
 

Users who are viewing this thread

Back
Top Bottom