How can i translate a DECIMAL into a Doted IP address

I am loocking for some code which will take a Decimal and return me a formated IP Address like 192.168.1.1 as a string..

AlexPonnathAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZeonFlashCommented:
Can you give an example of the "decimal" that you want translated?

Remember that passing a number like 19216811 can create IP's such as:  19.216.81.1, 1.92.168.11, 192.16.81.1, and so on.
amit_gCommented:
               Dim MyIPAddress As System.Net.IPAddress
               
                MyIPAddress = New System.Net.IPAddress(16777343)
                Trace.Warn(MyIPAddress.Address)
                Trace.Warn(MyIPAddress.ToString)

                MyIPAddress  = System.Net.IPAddress.Parse("127.0.0.1")
                Trace.Warn(MyIPAddress.Address)
                Trace.Warn(MyIPAddress.ToString)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AlexPonnathAuthor Commented:
that works great but is there a function which lets me reverse the nbr's easyly so for example

127.0.0.1 turn it in to a 1.0.0.127

i know i could split it based on the "." and the reorder them but mybe there is a cleaner way...

Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

amit_gCommented:
               MyIPAddress = New System.Net.IPAddress(System.Net.IPAddress.NetworkToHostOrder(16777343))
                Trace.Warn(MyIPAddress.Address)
                Trace.Warn(MyIPAddress.ToString)
AlexPonnathAuthor Commented:
that seems to not work correctly...
It works with the sample of 127.0.0.1 and 1.0.0.127 but for example if i use 216.158.230.2 and 2.230.158.216 it does not the Decimal for
2.230.158.216 = 48668376

if i use MyIPAddress = New System.Net.IPAddress(System.Net.IPAddress.NetworkToHostOrder(48668376))

i get the folowing error..

System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: newAddress
   at System.Net.IPAddress..ctor(Int64 newAddress)
   at VBDnsQuery.Form1.btnButton1_Click(Object sender, EventArgs e)
amit_gCommented:
Actually that method is incorrect. I don't think there is any method to do this. You will probably have to use the split and join.
Jayadev NairApplication DeveloperCommented:
If you want to only reverse the string you gets, use
StrReverse("1.0.0.127")
Which will give you reverse output.
Fernando SotoRetiredCommented:
Hi AlexPonnath;

To reverse the number order you can do it like this:

System.Text.RegularExpressions

    Dim ip As String = "127.0.0.1"
    ip = Regex.Replace(ip, "(\d+)\.(\d+)\.(\d+)\.(\d+)", "$4.$3.$2.$1")

ip will now contain 1.0.0.127

Fernando
AlexPonnathAuthor Commented:
Thanks....

any thought if there is a big performance diff between using your RegExpr or my split below ?

myString = Split(MyIPAddress.ToString, ".")
Return myString(3) & "." & myString(2) & "." & myString(1) & "." & myString(0)
Fernando SotoRetiredCommented:
Just a bit slower.
grayeCommented:
Here is what I use to convert a "quad dot" IP address to and from a string/long

    ' VB.Net doesn't support a Union type, but it does allow you to
    ' "decorate" a structure to accomplish the same thing
    <StructLayout(LayoutKind.Explicit)> _
    Private Structure IP_Parts
        <FieldOffset(0)> Dim address As Int64
        <FieldOffset(3)> Dim Right As Byte
        <FieldOffset(2)> Dim middleRight As Byte
        <FieldOffset(1)> Dim middleLeft As Byte
        <FieldOffset(0)> Dim Left As Byte
    End Structure

    Private Shared Function IPStrToLong(ByVal IPstr As String) As Long
        Dim ip As IPAddress
        Dim parts As IP_Parts
        Dim buf() As String

        ip = [IPAddress].None
        If Not [IPAddress].TryParse(IPstr, ip) Then
            Return 0
        End If

        ' The IPAddress.Address property has been "depreciated", but we still
        ' need a way to convert a IPv4 "quad dot" to and from a long, so just
        ' keep the compiler happy, we use this routine

        buf = ip.ToString.Split("."c)
        If buf.Length <> 4 Then
            ' Sorry, we don't support IPv6 notation
            Return 0
        End If

        parts.Right = CByte(buf(3))
        parts.middleRight = CByte(buf(2))
        parts.middleLeft = CByte(buf(1))
        parts.Left = CByte(buf(0))

        Return parts.address
    End Function

    Private Shared Function IPLongToStr(ByVal IPlong As Long) As String
        Dim parts As IP_Parts
        Dim buf As String

        parts.address = IPlong
        buf = parts.Left & "." & parts.middleLeft & "." & parts.middleRight & "." & parts.Right

        Return buf
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.