Copy Code code as follows:
Package net;
Import java.net.*;
/*
* The GetAddress method is similar to Gethostaddress, and the only difference is that the Gethostaddress method returns an IP address in the form of a string.
* and the GetAddress method returns an IP address in the form of a byte array.
* The range of byte types in Java is -128?127. If a byte of the returned IP address is an integer greater than 127, a negative number is in the byte array.
* Because there is no unsigned byte type in Java, you must use an int or a long type to display a normal IP address.
*/
public class MyIp
{
public static void Main (string[] args) throws Exception
{
InetAddress ia = inetaddress.getbyname ("www.cnblogs.com");
byte ip[] = Ia.getaddress ();
/*
for (byte Part:ip)
System.out.print (part + "");
System.out.println ("");
for (byte Part:ip)
{
int NEWIP = (Part < 0)? 256 + Part:part;
System.out.print (Newip + "");
}
*/
int[] array = new INT[5];
for (int i=0; i<ip.length; i++) {
Array[i] = (Ip[i] < 0)? 256 + ip[i]: ip[i];
}
String str = telliptype (array[0]);
System.out.println (str);
}
/*
* To determine the IP address type based on the first byte
*/
public static String telliptype (int num) {
if (num<127)
return "A";
else if (num<192)
return "B";
else if (num<224)
return "C";
else if (num<240)
return "D";
Else
return "E";
}
}