Link to home
Start Free TrialLog in
Avatar of AlexPonnath
AlexPonnathFlag for United States of America

asked on

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

Avatar of ZeonFlash
ZeonFlash

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.
ASKER CERTIFIED SOLUTION
Avatar of amit_g
amit_g
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AlexPonnath

ASKER

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

               MyIPAddress = New System.Net.IPAddress(System.Net.IPAddress.NetworkToHostOrder(16777343))
                Trace.Warn(MyIPAddress.Address)
                Trace.Warn(MyIPAddress.ToString)
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)
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.
If you want to only reverse the string you gets, use
StrReverse("1.0.0.127")
Which will give you reverse output.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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)
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