Convert IPAddress to byte array in CSharp

Description

The following code shows how to convert IPAddress to byte array.

Example


using System;/*  w w w  .  j a va2 s.  c  o m*/
using System.Net;
using System.Net.Sockets;

public class Example
{
   public static void Main()
   {
        string ipAddress = "127.0.0.1";
        IPAddress address = IPAddress.Parse(ipAddress);
        Byte[] bytes = address.GetAddressBytes();
        for (int i = 0; i < bytes.Length; i++) 
        {
          Console.Write(bytes[i]);
        }
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Network »




IP
Web Client