Open In App

Program to convert Byte array to IP Address

Last Updated : 10 Apr, 2023
Like 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 a 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 the computer on the 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 the ‘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 the 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
#include <bits/stdc++.h>
using namespace std;
int main()
    // Initialize the byte array
    unsigned char add[] = { 172, 31, 102, 14 };
    // Convert the byte array to an IP address string
    string IPAddress
        = to_string(add[0]) + "." + to_string(add[1]) + "."
          + to_string(add[2]) + "." + to_string(add[3]);
    // Print the IP address
    cout << IPAddress << endl;
    return 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*/


# Python code to convert Byte array
# to IP Address
# Example usage
add = [172, 31, 102, 14]
# Convert the byte array to an IP
# address string
IPAddress = str(add[0]) + "." + str(add[1]) + "." + \
    str(add[2]) + "." + str(add[3])
# Printing the IP Address


// Initialize the byte array
let add = [172, 31, 102, 14];
// Convert the byte array to an IP address string
let IPAddress = add.join('.');
// Print the IP address


public class ByteArrayToIPAddress {
    public static void main(String[] args)
        // Initialize the byte array
        byte[] add = new byte[] { (byte)172, (byte)31,
                                  (byte)102, (byte)14 };
        try {
            // Convert the byte array to an IP address
            InetAddress ipAddress
                = InetAddress.getByAddress(add);
            // Print the IP address
        catch (UnknownHostException e) {


Time Complexity: O(N)
Auxiliary Space: O(N)

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads