[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 957
  • Last Modified:

IP Number to IP address

I need a function that converts ip numer into IP address.

ipnum = 16777216*w + 65536*x + 256*y + z
where
IP Address = w.x.y.z
0
mxpoint
Asked:
mxpoint
1 Solution
 
bchoorCommented:
I'm pretty sure there are several other ways this can be done --

you can just shift, multiply and subtract

w = ipnum >> 24
ipnum = ipnum - (w * 2^24)
x = ipnum >> 16
ipnum = ipnum - (x * 2^16)
I posted an example at the bottom of this post
and so forth

or Maybe something like this
    Public Function IpNumberToIpAddress(ByVal IpNumber As Int64) As Integer()
        Dim IpAddress As New ArrayList

        For Each m As Match In Regex.Matches(IpNumber.ToString("x0").ToUpper(), "[\dA-F]{2}")
            IpAddress.Add(CType("&H" & m.Value, Integer))
        Next

        Return IpAddress.ToArray(GetType(System.Int32))
    End Function

Usage:
?IpNumberToIpAddress(3232235521.0)
{Length=4}
    (0): 192
    (1): 168
    (2): 0
    (3): 1


    Public Function IpNumberToIpAddress2(ByVal IpNumber As Int64) As Integer()
        Dim IpAddress As New ArrayList
        Dim v As Integer = 0

        v = IpNumber >> 24
        IpAddress.Add(v)
        IpNumber = IpNumber - (v * 2 ^ 24)

        v = IpNumber >> 16
        IpAddress.Add(v)
        IpNumber = IpNumber - (v * 2 ^ 16)

        v = IpNumber >> 8
        IpAddress.Add(v)
        IpNumber = IpNumber - (v * 2 ^ 8)

        v = IpNumber
        IpAddress.Add(v)

        Return IpAddress.ToArray(GetType(System.Int32))
    End Function
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now