Frage

I want to replace the last octet of an IP v4 for another new octet.

For example, if I have below IP v4:

192.168.0.100

and I want the last octet to be 200:

192.168.0.200

What is the best way to do it using Linq or regular expressions?

War es hilfreich?

Lösung

UPDATED ANSWER:

Just try with this...

        var bytes = IPAddress.Parse("192.168.1.33").GetAddressBytes();
        // set the value here
        bytes[3] = 100;

        IPAddress ipAddress = new IPAddress(bytes);

OR

        var bytes = IPAddress.Parse("192.168.1.33").GetAddressBytes(); 
        // set the value here
        bytes[3] = 100;

        System.Text.StringBuilder ipAddress = new System.Text.StringBuilder();
        foreach (byte b in bytes)
        {
            ipAddress.AppendFormat("{0}.", b);
        }

       string ipAddress1 = ipAddress.ToString();
       ipAddress1 = ipAddress1.TrimEnd('.');

       var newIp = IPAddress.Parse(ipAddress1);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top