DotNet Mirror
  DNM facebook   DNM Google+   DNM Twitter   

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

By Ashok Nalam on 15 Dec 2012 | Category: C# | Tagged: ip-address binary converter 
In this snippet we will see how to convert an IP address to binary format using System.Net.IPAddress class.
  Discuss Add Comment   |  
Add rating Rate this resource   

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
  Discuss Add Comment    
Add rating Rate this resource   
About the Contributor
Member Since : 10 Dec 2012
Member Points (Level) : 8666  (Professional)
Location : INDIA
Home Page : http://dotnetmirror.com
About : I am admin of this site.
Rate this resource
 
Add your Comment
Name Email WebSite
Captcha Refresh


Comments (0)
No comments found, click here to add comment.