Long2IP Function?

  • Thread starter Thread starter Jack
  • Start date Start date
J

Jack

Hello,

I need a long number to IP Address Convertion Function.

Take

3349551105

to

199.166.24.1

Please let me know if anyone has such a function.

Thanks in advance for your help,

Jack
 
Hello,

I need a long number to IP Address Convertion Function.

Take

3349551105

to

199.166.24.1

Please let me know if anyone has such a function.

Thanks in advance for your help,

Jack

Dim IP As String = New IPAddress (myLongValue).ToString ()
 
Hello Tom,

Thanks, it works great. There is one problem it is printing the IP address
backwords.

199.166.24.1

Comes out

1.24.166.199

Please let me know if how to fix this.

Thank you for all of your help,

Jack
 
Hello Tom,

Thanks, it works great. There is one problem it is printing the IP address
backwords.

199.166.24.1

Comes out

1.24.166.199

Please let me know if how to fix this.

Thank you for all of your help,

Jack

Ooops... Try this:

Dim IP As String = New IPAddress _
(IPAddress.NetworkToHostOrder (MyLongValue)).ToString()
 
Hello Tom,

Thanks for the fast response. I am getting this error using the new
command:

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred
in system.dll

Please let me know what I am doing wrong.

Thanks for your help and time,

Jack
 
Hello Tom,

Thanks for the fast response. I am getting this error using the new
command:

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred
in system.dll

Please let me know what I am doing wrong.

Thanks for your help and time,

Jack

Hmm... Jack - let me play with this some more (if no one else jumps in).
This is what I get for not testing my responses :)
 
Hello Tom,

Thanks for the fast response. I am getting this error using the new
command:

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred
in system.dll

Please let me know what I am doing wrong.

Thanks for your help and time,

Jack

Ok... One more shot :)

Option Strict On
Option Explicit On

Imports System.Net

Module Module1

Sub Main()
Dim tmpIP As New IPAddress(3349551105)
Dim bytes() As Byte = tmpIP.GetAddressBytes()
Array.Reverse(bytes)
Dim addr As Long = BitConverter.ToInt32(bytes, 0)
Console.WriteLine(New IPAddress(addr).ToString())
Console.ReadLine()
End Sub

End Module
 
Thank you very much!!!

Jack

Tom Shelton said:
Ok... One more shot :)

Option Strict On
Option Explicit On

Imports System.Net

Module Module1

Sub Main()
Dim tmpIP As New IPAddress(3349551105)
Dim bytes() As Byte = tmpIP.GetAddressBytes()
Array.Reverse(bytes)
Dim addr As Long = BitConverter.ToInt32(bytes, 0)
Console.WriteLine(New IPAddress(addr).ToString())
Console.ReadLine()
End Sub

End Module
 
Hello,

I have made a function out off the info:

Function IPNumber2IPAddress(ByVal IP As Long) As String

Dim tmpIP As New System.Net.IPAddress(IP)

Dim bytes() As Byte = tmpIP.GetAddressBytes()

Array.Reverse(bytes)

Dim addr As Long = BitConverter.ToInt32(bytes, 0)

IPNumber2IPAddress = New System.Net.IPAddress(addr).ToString()

End Function

It work most of the time, but when the IP Number is : 1167456428

I get an 'ArgumentOutOfRangeException' Error.

Any help would be great!!

Thanks,

Jack
 
Jack said:
Hello,

I have made a function out off the info:

Function IPNumber2IPAddress(ByVal IP As Long) As String

Dim tmpIP As New System.Net.IPAddress(IP)

Dim bytes() As Byte = tmpIP.GetAddressBytes()

Array.Reverse(bytes)

Dim addr As Long = BitConverter.ToInt32(bytes, 0)

IPNumber2IPAddress = New System.Net.IPAddress(addr).ToString()

End Function

It work most of the time, but when the IP Number is : 1167456428

I get an 'ArgumentOutOfRangeException' Error.

Any help would be great!!

Change

Dim addr As Long = BitConverter.ToInt32(bytes, 0)

to

Dim addr As Long = Convert.ToInt64(BitConverter.ToUInt32(bytes, 0))

as with high IP Numbers you were getting negative Integers from
ToInt32.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top