Introduction
In this snippet we will see how to convert an IP address to binary format using System.Net.IPAddress class.
IPAddress class contains the address of a computer on an IP network and supports IPv4 or IPv6.
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