Copy codeThe Code is as follows: package net;
Import java.net .*;
/*
* The getAddress method is similar to the getHostAddress method. The only difference between the getHostAddress method and getHostAddress method is that the getHostAddress method returns a string-type IP address,
* The getAddress method returns an IP address in byte array format.
* In Java, the value range of the byte type is-128? 127. If a byte of the returned IP address is an integer greater than 127, It is a negative number in the byte array.
* Because Java does not have an unsigned byte type, int or long type must be used to display normal IP addresses.
*/
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 & lt; 127)
Return "";
Else if (num & lt; 192)
Return "B ";
Else if (num & lt; 224)
Return "C ";
Else if (num & lt; 240)
Return "D ";
Else
Return "E ";
}
}