Objects in this mirror are closer to Microsoft Technologies. DNM objective is to help users on Microsoft technologies by providing Articles, snippets, Interview Questions.

03 September 2012

convert IP address to binary format using IPAddress Class in C#


In this snippet we will see how to convert an IP address to binary format using System.Net.IPAddress class.

static List<string> ConvertIPAddressToBinary(string input)
        {
           IEnumerable<string> binaries= IPAddress.Parse(input).GetAddressBytes().Select( x=> Convert.ToString(Convert.ToInt32(x), 2).PadLeft(8, '0'));
           return binaries.ToList();
        }
 
  static void Main()
        {
            List<string> listBinaries = ConvertIPAddressToBinary(“192.168.0.1”)
            foreach (string binary in listBinaries)
            {
                Console.Write(binary + " ");
            }
        }
Output : 11000000 10101000 00000000 00000001

IPAddress class contains the address of a computer on an IP network and supports IPv4 or IPv6.

2 Comments:

Anonymous said...

Stunning quest there. What happened after? Thanks!


my weblog: online buying sites

Anonymous said...

bylice 9 F 2014-02-12 nike free 5.0 womens18:57:
10

Who in the end nike free 5.0 womensif this is true, then who is fake
ah.

-----------------------------

Do nike free run 3 womens mint greennot
put Lily gave place nike free run 3.0in my heart right denied.





Take a look at my web blog: nike free running shoes

Post a Comment