Open In App

Program to convert Byte array to IP Address

Last Updated : 10 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

Examples:

Input : {16, 16, 16, 16}
Output : 16.16.16.16
Input : {172, 31, 102, 14}
Output : 172.31.102.14

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: https://msdn.microsoft.com/en-us/library/dd126860.aspx 

Syntax:

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: http://snmpsharpnet.sourceforge.net/ver0-4/html/T_SnmpSharpNet_IpAddress.htm 

Syntax:

[Serializable]
public class IPAddress

Using Byte Arrays with IPAddress Class 

IPAddress ObjectName = new IPAddress(byte[])

Approach: 

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++




// 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#




// 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*/
        Console.WriteLine(add.ToString());
    }
}


Python3




# 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
print(IPAddress)


Javascript




// 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
console.log(IPAddress);


Java




import java.net.InetAddress;
import java.net.UnknownHostException;
 
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
            System.out.println(ipAddress.getHostAddress());
        }
        catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}


Output

172.31.102.14

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads