
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java program to convert Byte array to IP Address
Given with a Byte array ad the task is to convert it into an IP address using the IPAddress class in java and display the result.
What is a Byte Array
A byte comprises of 8 bits and byte array comprises of contiguous bytes which stores the binary information. In java, byte is a primitive datatype that can be understood as the byte of computer i.e. it is of 8 bits and it can hold values ranging for -128 to 127.
Declaring a byte − byte name_of_byte_variable = initializer;
Declaring a byte array − byte[] name_of_byte_array = new byte[];
What is an IPAddress Class
In java, IPAddress class is used to get the IP address of any system. It is present in the System.net class which needs to be imported to make use of IPAddress class.
Syntax
IPAddress ObjectName = new IPAddress(byte[])
Example
Input-: 171, 32, 101, 11 Output-: 171.32.101.11 Input-: 172, 31, 102, 14 Output-: 172.31.102.14
Approach we are using in the below program is as follows −
- Import the class System.net
- Input the numbers as the bytes in the byte array
- Create the object of class IPAddress and pass the byte array to its object
- Use function ToString() to convert the Address into string representation
- Print the result
ALGORITHM
START Step 1-> declare class convert for conversion public class convert call class public static void Main() set IPAddress add = new IPAddress(new byte[] { 171, 32, 101, 11 }) call Console.WriteLine(add.ToString()) End End STOP
Example
using System; using System.Net; public class convert { public static void Main() { IPAddress add = new IPAddress(new byte[] { 171, 32, 101, 11 }); Console.WriteLine(add.ToString()); } }
Output
171.32.101.11
- Related Articles
- Java Program to convert byte[] array to String
- Java Program to convert String to byte array
- Program to convert IP address to hexadecimal in C++
- Java program to display Hostname and IP address
- Java program to Get IP address of the system
- Java program to find IP Address of the client
- Java Program to convert byte to string
- Java Program to convert string to byte
- Golang program to convert file to byte array
- Convert byte Array to Hex String in Java
- Convert Hex String to byte Array in Java
- How to convert BLOB to Byte Array in java?
- How to convert Byte Array to Image in java?
- How to convert Image to Byte Array in java?
- How to convert InputStream to byte array in Java?

Advertisements