AlexPonnath
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..
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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...
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(Syste m.Net.IPAd dress.Netw orkToHostO rder(16777 343))
Trace.Warn(MyIPAddress.Add ress)
Trace.Warn(MyIPAddress.ToS tring)
Trace.Warn(MyIPAddress.Add
Trace.Warn(MyIPAddress.ToS
ASKER
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(Syste m.Net.IPAd dress.Netw orkToHostO rder(48668 376))
i get the folowing error..
System.ArgumentOutOfRangeE xception: Specified argument was out of the range of valid values.
Parameter name: newAddress
at System.Net.IPAddress..ctor (Int64 newAddress)
at VBDnsQuery.Form1.btnButton 1_Click(Ob ject sender, EventArgs e)
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(Syste
i get the folowing error..
System.ArgumentOutOfRangeE
Parameter name: newAddress
at System.Net.IPAddress..ctor
at VBDnsQuery.Form1.btnButton
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.
StrReverse("1.0.0.127")
Which will give you reverse output.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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)
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.E xplicit)> _
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
' VB.Net doesn't support a Union type, but it does allow you to
' "decorate" a structure to accomplish the same thing
<StructLayout(LayoutKind.E
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
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
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.