Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA Function Decimal back to IP 3

Status
Not open for further replies.
Jan 12, 2004
15
US
PHV
has a great function to convert an IP to its decimal equivalent.

Code:
Public Function ip2num(ip)
Dim i, a, n
a = Split(ip, ".")
n = CDbl(0)
For i = 0 To UBound(a)
  n = n * 256 + a(i)
Next i
ip2num = n
End Function
Works like a charm, now I am looking for a similar function
to convert a decimal to an IP address.

ip2num("127.0.0.1")=2130706433

So I am looking for...
num2ip(2130706433)=127.0.0.1
 
You could use something like:
Code:
Public Function num2ip(MyNum)
num2ip = Val("&H" & Mid(Hex(MyNum), 1, 2)) & "." & Val("&H" & Mid(Hex(MyNum), 3, 2)) _
     & "." & Val("&H" & Mid(Hex(MyNum), 5, 2)) & "." & Val("&H" & Mid(Hex(MyNum), 7, 2))
End Function

Sub TestIt()
    MsgBox num2ip(InputBox("Number to Convert"))
End Sub

Cheers
 
How about this?
Code:
Public Function Dec2IP(myNum)
    Dec2IP = (Int(myNum / 256 ^ 3) & "." & Int((myNum Mod (256 ^ 3)) / (256 ^ 2)) & "." & Int(((myNum Mod 256 ^ 3) Mod 256 ^ 2) / 256) & "." & Int((((myNum Mod 256 ^ 3) Mod 256 ^ 2) Mod 256)))
End Function
 
Hi,

Code revision follows - the original didn't correctly convert IP addresses where the first vlaue was < 16.

Code:
Public Function num2ip(MyNum)
Offset = 8 - Len(Hex(MyNum))
num2ip = Val("&H" & Mid(Hex(MyNum), 1, 2 - Offset)) & "." & Val("&H" & Mid(Hex(MyNum), 3 - Offset, 2)) _
     & "." & Val("&H" & Mid(Hex(MyNum), 5 - Offset, 2)) & "." & Val("&H" & Mid(Hex(MyNum), 7 - Offset, 2))
End Function

Cheers
PS: I posted this earlier, but it's not showing up!
 
Public Function num2ip(n)
Dim s, i, p, z
z = Int(n / 256)
s = Right("00000" & Hex(z), 6)
For i = 1 To 5 Step 2
p = p & Val("&h" & Mid(s, i, 2)) & "."
Next i
num2ip = p & Val("&h" & Hex(n - 256 * z))
End Function

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Both solutions have a problem if MyNum > &7FFFFFFF

here's an alternative:
Code:
[blue]Private Type DottedQuad
    Quad0 As Byte
    Quad1 As Byte
    Quad2 As Byte
    Quad3 As Byte
End Type

Private Type DecimalQuad
    QuadAll As Currency
End Type

Private Function num2ip(ByVal lNum As Double) As String
    Dim Dotted As DottedQuad
    Dim Proxie As DecimalQuad
    
    Proxie.QuadAll = lNum / 10000
    LSet Dotted = Proxie
    With Dotted
        num2ip = .Quad3 & "." & .Quad2 & "." & .Quad1 & "." & .Quad0
    End With
End Function[/blue]
 
With my posted version:
? num2ip(&H7FFFFFFF)
127.255.255.255

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Nice, Mike. Star from me.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top