Results 1 to 2 of 2

Thread: Ok You Math Wiz's...

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2002
    Posts
    382

    Ok You Math Wiz's...

    I need some help on IP Address incrementation. I have a few ways to possibly do it but I'm not 100% sure.

    One method would be to convert the IP address to a DWord list like here then increment the value by 1 then convert the dword back to IP? This seems to work but I'm not sure how to go about converting it to a dword and back again.

    Another method would be shifting the bits (as my c++ buddy says to do) but I am not quite sure what he means.

    Another way would be to use the Socket.Net.IPAddress.Address Int value but I don't understand how it increments..

    3568844760 = 216.63.184.212
    3568844761 = 217.63.184.212

    Changing the latter value by 1 changes the first Quad, I don't understand that one bit..

    Basically I just need to Step though a range of manually set IP addresses and I'm not sure just what would be the best way..

    Cheers to the person who can help me out with this

  2. #2
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    Ok, I did a dirty one here. You can build this alot better I guess but I did it quick. I have no code to check if it is a valid IP and such, it just increments a IP if it is a valid IP, you probably get alot of different errors if not.

    Hope it still helps....

    The general idea I used was to split the IP into 4 parts and individually change each "byte" to a Hexadecimal digit pair. Then concatenate the 4, change to Long, increment, go back to hex, split it into 4 again, and transfer back to Byte and concatenate them as strings (with a dot between each "Byte").

    Code:
    Private Function IncrementIP(ByVal IP As String) As String
    
        'LoopCounter
        Dim i As Integer
    
        'IP-strings array
        Dim s1() As String = Strings.Split(IP, ".")
        Dim s2(3) As String
    
        'Decimal values
        Dim d1 As Long
        Dim d2(3) As Long
    
        'Hexstring
        Dim h As String
    
        'Create Hexstring (pading string if hex-value has only 1 digit)
        For i = 0 To 3
            h += CStr(Hex(CByte(s1(i)))).PadLeft(2, "0")
        Next
    
        'Change from hex to long and increment
        d1 = "&H" & h
        d1 += 1
    
        'Change back from Decimal to hex
        h = Hex(d1)
    
        'Create new IP-string array
        s2(0) = Strings.Left(h, 2)
        s2(1) = Strings.Right(Strings.Left(h, 4), 2)
        s2(2) = Strings.Left(Strings.Right(h, 4), 2)
        s2(3) = Strings.Right(h, 2)
    
        'Convert to long
        For i = 0 To 3
            d2(i) = "&H" & s2(i)
        Next
    
        'Creat ne IP and return it
        Return CStr(d2(0)) & "." & CStr(d2(1)) & "." & CStr(d2(2)) & "." & CStr(d2(3))
    
    End Function
    Last edited by Athley; Jan 2nd, 2003 at 10:48 AM.

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