Java: converting a string (representing IP) to InetAddress

Possible duplicate:
Is there an easy way to convert String to Inetaddress in Java?

I am trying to convert a string (representing an IP address, e.g. 10.0.2.50 ) to an InetAddress obj object.

According to the API, you can create an object that provides a string that represents the host name (for example, www.google.ch ). This is not an option for me, since I do not have a host name for each InetAddress object that I want to create (except that it takes too much time).

Is it possible to convert a String (e.g. 10.0.2.50 ) into an InetAddress obj. Object? (according to the api, this can be done if you have an IP like byte[] , but how do I convert a String containing an IP address to byte[] ?)

+50
java ip
Apr 6 '11 at 19:15
source share
2 answers

Just call InetAddress.getByName(String host) , passing in your text IP address.

From javadoc: The host name can be either the name of a machine, for example, "java.sun.com", or the textual representation of its IP address.

Inetaddress javadoc

+116
Apr 6 2018-11-11T00:
source share

From the InetAddress.getByName(String host) documentation:

The host name can be a machine name, for example "java.sun.com", or a textual representation of its IP address. If the literal IP address is provided only the validity period of the address format.

So you can use it.

+11
Apr 6 2018-11-11T00:
source share



All Articles