Skip to content
Related Articles

Related Articles

Program to convert Byte array to IP Address

View Discussion
Improve Article
Save Article
  • Last Updated : 24 Apr, 2018
View Discussion
Improve Article
Save Article

Given a Byte Array, convert it to the format of IP Address.


Input : {16, 16, 16, 16}
Output :

Input : {172, 31, 102, 14}
Output :

Byte arrays:
A byte is collection of bits (8). Byte arrays are arrays of contiguous bytes and can be used to store binary information. With byte arrays, one can work directly on bytes and thus control the bits. Byte arrays provide a faster way of accessing each byte in use.
For more details:


byte[] ArrayName = new byte[] 

IPAddress Class:
The IPAddress class contains the address of computer on IP network. IPAddress class accommodates IP Address values passed to or returned by Simple Network Management Protocol (SNMP) agents by extending the OctetString Class. IPAddress Class comes under ‘System.Net’ namespace.

Refer Link for details:


public class IPAddress

Using Byte Arrays with IPAddress Class

IPAddress ObjectName = new IPAddress(byte[])

The IPAddress Class is used to get the IP address. The IP Address is created with the Address property set to address. If the length of address is 4, IPAddress(Byte[]) constructs an IPv4 address otherwise, an IPv6 address with a scope of 0 is constructed. The Byte array is assumed to be in network byte order with the most significant byte in index position 0.

// C# code to convert Byte array to IP Address
using System;
using System.Net;
public class GFG {
    public static void Main()
        /* Initializes a new instance of the 
        IPAddress class with the address 
        specified as a Byte Array.*/
        IPAddress add = new IPAddress(new byte[] 
                            { 172, 31, 102, 14 });
        /* ToString() Converts an Internet address
        to its standard notation and 
        console.WriteLine is used for printing*/

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!