-2

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?

user304602
  • 991
  • 4
  • 21
  • 39
  • *Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work* – L.B Sep 29 '13 at 11:30

1 Answers1

4

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);
Thilina H
  • 5,754
  • 6
  • 26
  • 56