|
-
Jan 7th, 2007, 04:02 AM
#1
Thread Starter
Lively Member
[2005] Convert IP address decimal to dotted quad
Hello
Does anyone know how to do this is vb2005?
Thanks :-)
-
Jan 7th, 2007, 04:49 AM
#2
Re: [2005] Convert IP address decimal to dotted quad
-
Jan 7th, 2007, 05:00 AM
#3
Thread Starter
Lively Member
Re: [2005] Convert IP address decimal to dotted quad
Thanks
I have looked at the class in msdn but it went way over my head (newbie here)
Could you explain a little more?
I did a find for decimal on teh page also but did not find anything
Thanks
-
Jan 7th, 2007, 05:17 AM
#4
Re: [2005] Convert IP address decimal to dotted quad
What exactly are you starting with? Number? String? What format? What exactly do you want to end up with? Example?
-
Jan 7th, 2007, 05:21 AM
#5
Thread Starter
Lively Member
Re: [2005] Convert IP address decimal to dotted quad
Hello
I start with decimal 3420263445
need to end up with 203.221.20.21
Thanks :-)
I found this..
The easiest way to do it is to depict the ip address in binary. Take 3420263445 and convert to binary using any calculator that will do decimal to binary conversion (like the one that comes with Windows/95/98/NT):
binary address: 11001011.11011101.00010100.00010101
Then convert each octet back to decimal and you get: 203.221.20.21
But I don't know how to do that either :-(
-
Jan 7th, 2007, 05:39 AM
#6
Re: [2005] Convert IP address decimal to dotted quad
VB Code:
Dim ip As New Net.IPAddress(3420263445)
Dim address As String = ip.ToString()
This code returns the address 21.20.221.203. You are obviously using a value that has the opposite endedness to what the IPAddress class is looking for. To fix that you can convert the original number to bytes and reverse the order:
VB Code:
Dim bytes As Byte() = BitConverter.GetBytes(3420263445[U]UI[/U])
Array.Reverse(bytes)
Dim ip As New Net.IPAddress(bytes)
Dim address As String = ip.ToString()
Note the "UI" suffix on the number to force it to an unsigned integer. If you don't do that it will be interpreted as a Long and the Byte array returned by GetBytes will be 8 bytes long instead of 4.
-
Jan 7th, 2007, 05:49 AM
#7
Thread Starter
Lively Member
Re: [2005] Convert IP address decimal to dotted quad
That is perfect!
Thanks once again for answering my question.
Do you know how to go back again?
So if I started with 203.221.20.21
Sorry if I am pushing my luck!
Thanks
-
Jan 7th, 2007, 06:02 AM
#8
Thread Starter
Lively Member
Re: [2005] Convert IP address decimal to dotted quad
This may be a way
http://www.spiderhunter.com/IpDecimal/
Your IpAddress is 150.101.162.165:
1. 150 * 256 + 101 = 38,501
2. 38501 * 256 + 162 = 9,856,418
3. 9856418 * 256 + 165 = 2,523,243,173
-
Jan 8th, 2007, 02:49 AM
#9
Addicted Member
Re: [2005] Convert IP address decimal to dotted quad
Hi,
VB Code:
Dim ip As IPAddress = Dns.GetHostAddresses(Dns.GetHostName())(0)
MessageBox.Show(ip.ToString)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|