Monday, March 23, 2009

How do I convert an ip address into an array of bytes?

Use the getAddress() method in the InetAddress class.

InetAddress address =
InetAddress.getByName("192.168.1.1");
byte[] bytes = address.getAddress();
for (byte b : bytes)
{
System.out.println(b & 0xFF);
}

No comments: