Jump to content
Sign in to follow this  
BlueKit

increment IP + 10

Recommended Posts

BlueKit

Hi all,

I write an ip address in a GUI,

or i read this IP on SQLite

ex : $IPAddress=192.168.2.200 (ip of a server)

How can i have IP address +10 for configure the host

ex : 192.168.2.210

Thanks for your help

Share this post


Link to post
Share on other sites
Mat

Use StringSplit to split the IP address into its parts, then add ten to the last one ;)

$asSplit = StringSplit($IPAddress, ".")

$asSplit[4] += 1

$IPAddress = $asSplit[1] & "." & $asSplit[2] & "." & $asSplit[3] & "." & $asSplit[4]

Share this post


Link to post
Share on other sites
Calistoga

As Mat said ;)

MsgBox(64, "IP Increment", _IP_Add("192.168.2.200", 10));

Func _IP_Add($s_IP = "192.168.2.200", $i = 10)

    Local $a_parts = StringSplit($s_IP, ".", 0);

    If (IsArray($a_parts) = 0 Or $a_parts[0] <> 4) Then Return SetError(1, 0, "");

    $a_parts[4] += $i;

    $s_IP = $a_parts[1] & "." & $a_parts[2] & "." & $a_parts[3] & "." & $a_parts[4];

    Return SetError(0, 0, $s_IP);

EndFunc ;==>_IP_Add

Share this post


Link to post
Share on other sites
BlueKit

Thanks for yours answers

Share this post


Link to post
Share on other sites
Mat

Just in case you need this, it adds it up properly, so '0.0.0.0 + 256' = '0.0.1.0'.

MsgBox(64, "IP Increment", _IP_Add("192.168.2.200", 60))

Func _IP_Add($s_IP, $i)
    Local $a_parts = StringSplit($s_IP, ".", 0)
    If Not IsArray($a_parts) Or $a_parts[0] <> 4 Then Return SetError(1, 0, "") ; Incorrect input.

    $a_parts[4] += $i

    For $n = 4 To 1 Step -1
        $a_parts[$n - 1] += Floor($a_parts[$n] / 256)
        $a_parts[$n] = Mod($a_parts[$n], 256)
    Next

    If $a_parts[0] <> 4 Then Return SetError(2, 0, "") ; Out of range.

    $s_IP = StringFormat("%.3s.%.3s.%.3s.%.3s", $a_parts[1], $a_parts[2], $a_parts[3], $a_parts[4])
    Return $s_IP
EndFunc ;==>_IP_Add

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×