Convert and Display IP Address to Binary form?

c#

Solution

static string IPAddrToBinary( string input) {
   // assumes a valid IP Address format
   return String.Join(".", (input.Split('.').Select(x => Convert.ToString(Int32.Parse(x), 2).PadLeft(8, '0'))).ToArray());
}

Here's a version with comments, which may be a little easier to understand:

static string IPAddrToBinary(string input)
{
    return String.Join(".", ( // join segments
        input.Split('.').Select( // split segments into a string[]

            // take each element of array, name it "x",
            //   and return binary format string
            x => Convert.ToString(Int32.Parse(x), 2).PadLeft(8, '0')

        // convert the IEnumerable<string> to string[],
        // which is 2nd parameter of String.Join
        )).ToArray());
}

Problem

Is there a method in c# that would talk the ip address 10.13.216.41 and display it as 00001010.00001101.11011000.00101001. If not, how can it be done?

Original source