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 as a string..

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.

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:,,, and so on.
               Dim MyIPAddress As System.Net.IPAddress
                MyIPAddress = New System.Net.IPAddress(16777343)

                MyIPAddress  = System.Net.IPAddress.Parse("")

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 turn it in to a

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.

               MyIPAddress = New System.Net.IPAddress(System.Net.IPAddress.NetworkToHostOrder(16777343))
AlexPonnathAuthor Commented:
that seems to not work correctly...
It works with the sample of and but for example if i use and it does not the Decimal for = 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)
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
Which will give you reverse output.
Fernando SotoRetiredCommented:
Hi AlexPonnath;

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


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

ip will now contain

AlexPonnathAuthor Commented:

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.
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.