이 기사의 예는 Java의 IP 및 Integer 방법을 알려줍니다. 참조를 위해 모든 사람을 위해 공유하십시오. 특정 분석은 다음과 같습니다.
1. 기본 지식 포인트
IP-> 정수 :
IP 주소를 왼쪽 시프트 (<<), (&), 또는 (|) int to int로 (|)를 통해 바이트 배열로 변환합니다.
정수-> IP :
오른쪽 시프트 작동 (>>>) 오른쪽을 24 비트로 전환 한 다음 획득 한 숫자는 첫 번째 IP입니다.
오른쪽 시프트 작동 (>>>)은 16 비트를 오른쪽으로 바꾸고 연산자 (&) 0xff를 수행합니다.
오른쪽 시프트 작동 (>>>), 8 비트의 오른쪽으로 이동 한 다음, 획득 한 숫자는 세 번째 세그먼트 IP입니다.
정수 값은 연산자 (&) 0xff와 함께 수행되며, 얻은 숫자는 네 번째 단계 IP입니다.
둘째, Java 코드 예제 (ipv4util.java)
Michael.Utils; } catch (예외 E) {New Impaddr + "는 잘못된 IP"{byte [] ret = new Byte [4]; . "); ret [0] = (byte) (integer.parseint (iparr [0]); ret [1] = (byte) (integer. parseint (iparr [1]) & 0xff; ret [2] = ( byte) (integer.parseint (iparr [2]); ret [3] = (byte) (iparr [3]) & 0xff)} catch (exception e); ip "); t [] bytes) {return new StringBuffer (). Append (Bytes [0] & 0xff) .append ( '.'). Append (Bytes [1] & 0xff) .appnd ( '.'). Append (bytes [2] & 0xff) .appnd ( '.') .tring ();} public static intostoint (byte [] bytes) {int addr = bytes [3] & 0xff; addr | | let atic byte [] intbytes (int ipint) {byte [] ipaddr = new byte [inaddrsz]; (iPint >>> 16) & 0xff); 정적 문자열 inttoip (int ipint) {return newbuilder () .append ((((((() (((((((() .appnd (iPint >> 16). Appnd ( '.'). '). String [] iparr = ipandmask.split ( "/"); trim ()); if (netma sk <0 || netmask> 31) { " + ipandmask);} int ipint = ipv4util.iptoint (ipv4util.iptoint); & (0xffffffffffff <(3 3 2 -netmask); int hostscope = (0xffffffff >>> netmask); 새로운 int [] {netip, netip + hostscope};} public static string [] getipaddrscope (String ipandmask) {int ] ipintarr = getipintscope (ipandmask); ) {int netmaskint = 0; ipint};} netmaskint = ipv4util.ips ( "255.255.255.255") -NetMaskint; hostscope};} h (예외 e) {새로운 불법 불법 행위 exception 던지 [] ipv4util.getipintscope (ip addr, mask); return new String [] {ipv4util .inttoip (ipintarar [0]), ipv4util.inttoip (ipintarr [0]);} public static void main (String [] args). 예외 {문자열 ipaddr = "192.168.8.1"bytearr = ipv4util (ipaddr); ) {bytestr.append (b);} else {bytest.append ( "," "," " + b);}} system.out.println ("ip : " + iPaddr +"byte-> byte [] : [ + bytestr + "); bytearr = ipv4util.iptobyreg (bytest); );} else {bytestr.append ( "," + b);}} system.out .println ( "ip :" + iPaddr + "byreg-> byte [] : [" + bytestr + "]; System. out.println ( "byte [] :" + bytest + " -> ip :" + ipv4util.bytestoip (bytearr)); + " ->" + iPint); "int : + ipint +" -> ip : " + ipv4util.inttoip (ipint)); String ipandmask ="192.168.1.1/24 "; int [] ipscope = ipv4util.getipintscope (ipandmask) ;; int 주소 세그먼트 : [ " + ipscope [0] +", " + ipscope [1] +"]; + "-> IP 주소 세그먼트 : [ + ipv4util.inttoip (ipscope [0]) +", " + ipv4util.inttoip (ipscope [1])); String ipaddr1 ="192.168.1.1 ", ipmask1 ="255.255.255.0 "; int []] ","ipScope1 [1] + "]; system.out.println (ipaddr1 +", " + ipmask1 +" -> ip 주소 세그먼트 : [ " + ipv4util.inttoip (ipscope1 [0]) +", " +" + ipv4util.inttoip (ipscope1 [1]) + "]" ");}}
이 기사는 모든 사람의 Java 프로그램 설계에 도움이되기를 바랍니다.