Results 1 to 9 of 9

Thread: [2005] Convert IP address decimal to dotted quad

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Posts
    101

    [2005] Convert IP address decimal to dotted quad

    Hello

    Does anyone know how to do this is vb2005?

    Thanks :-)

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Convert IP address decimal to dotted quad

    The IPAddress class.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Posts
    101

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Posts
    101

    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 :-(

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Convert IP address decimal to dotted quad

    VB Code:
    1. Dim ip As New Net.IPAddress(3420263445)
    2. 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:
    1. Dim bytes As Byte() = BitConverter.GetBytes(3420263445[U]UI[/U])
    2.  
    3. Array.Reverse(bytes)
    4.  
    5. Dim ip As New Net.IPAddress(bytes)
    6. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Posts
    101

    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

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Posts
    101

    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

  9. #9
    Addicted Member
    Join Date
    Aug 2002
    Posts
    224

    Re: [2005] Convert IP address decimal to dotted quad

    Hi,

    VB Code:
    1. Dim ip As IPAddress = Dns.GetHostAddresses(Dns.GetHostName())(0)
    2. 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
  •  



Click Here to Expand Forum to Full Width