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++
#include <bits/stdc++.h>
using namespace std;
int main()
{
unsigned char add[] = { 172, 31, 102, 14 };
string IPAddress
= to_string(add[0]) + "." + to_string(add[1]) + "."
+ to_string(add[2]) + "." + to_string(add[3]);
cout << IPAddress << endl;
return 0;
}
|
C#
using System;
using System.Net;
public class GFG {
public static void Main()
{
IPAddress add = new IPAddress(
new byte [] { 172, 31, 102, 14 });
Console.WriteLine(add.ToString());
}
}
|
Python3
add = [ 172 , 31 , 102 , 14 ]
IPAddress = str (add[ 0 ]) + "." + str (add[ 1 ]) + "." + \
str (add[ 2 ]) + "." + str (add[ 3 ])
print (IPAddress)
|
Javascript
let add = [172, 31, 102, 14];
let IPAddress = add.join( '.' );
console.log(IPAddress);
|
Java
import java.net.InetAddress;
import java.net.UnknownHostException;
public class ByteArrayToIPAddress {
public static void main(String[] args)
{
byte [] add = new byte [] { ( byte ) 172 , ( byte ) 31 ,
( byte ) 102 , ( byte ) 14 };
try {
InetAddress ipAddress
= InetAddress.getByAddress(add);
System.out.println(ipAddress.getHostAddress());
}
catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
10 Apr, 2023
Like Article
Save Article