Copy the code code as follows:
package net;
import java.net.*;
/*
* The getAddress method is similar to getHostAddress. The only difference is that the getHostAddress method returns an IP address in the form of a string.
* The getAddress method returns the IP address in the form of a byte array.
* The value range of byte type in Java is -128?127. If a certain byte of the returned IP address is an integer greater than 127, it will be a negative number in the byte array.
* Since there is no unsigned byte type in Java, in order to display a normal IP address, the int or long type must be used.
*/
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);
}
/*
* 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";
}
}