يحكي مثال هذه المقالة طريقة IP و INTEGER في Java. شاركه للجميع للرجوع إليه. التحليل المحدد هو كما يلي:
1. نقاط المعرفة الأساسية
IP-> integer:
تحويل عنوان IP إلى صفيف البايت من خلال التحول الأيسر (<<) ، (&) ، أو (|) هذه العمليات إلى int إلى int
عدد صحيح-> IP:
عملية التحول الصحيح (>>>) ، قم بتحويل اليمين إلى 24 بت ، ثم قم بتنفيذ المشغل (&) 0xFF.
عملية التحول الصحيح (>>>) ، Shift 16 بت من اليمين ، ثم قم بإجراء المشغل (&) 0xFF.
عملية التحول الصحيح (>>>) ، انتقل إلى يمين 8 بت ، ثم قم بإجراء المشغل (&) 0xFF.
يتم تنفيذ قيمة عدد صحيح مع المشغل (&) 0xFF ، والرقم الذي تم الحصول عليه هو IP المرحلة الرابعة.
ثانياً ، مثال رمز Java (IPv4util.java)
حزمة michael.utils ؛ } catch (استثناء e) {رمي غير alfictalargumentexception (ipaddr + "IP غير صالح") ؛ . بايت) (integer.parseint (iparr [2]) ؛ ret [3] = (byte) (iparr [3]) & 0xff) ؛ IP ") ؛ te [] bytes) {return new StringBuffer (). إلحاق (bytes [0] & 0xff) .append ('.'). إلحاق (bytes [1] & 0xff) .appnd ('.'). إلحاق (Bytes [2] & 0xff) .appnd ('.'). addr | ؛ atic byte [] intbytes (int ipint) {byte [] ipaddr = new byte [inaddrsz] ؛ (ipint >> 16) سلسلة ثابتة inttoip (int ipint) {return newbuilder () .Append ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((( ((((((((((((((((((. .appnd ((ipint >> 16) String [] iparr = ipandmask.split ("/") ؛ trim ()) ؛ & (0xffffffffffff <(3 3 2 -netmask) ؛ int hostscope = (0xffffffffffff >>> netmask) ؛ إرجاع int [] {netip ، netip + hostscope} ؛ ] ipintarr = ipv4util ) {int ipint ؛ ipint} ؛ HostScope} ؛} h (استثناء e) {رمي جديد alficalaRgumentedException ("Scope Invalid Scope Express IP:" + ipaddr + "قناع:" + قناع) ؛}} سلسلة ثابتة عامة [] [] IPv4util.getipintscope (IP addr ، Mask) ؛ رمي {string ipaddr = "192.168.8" ؛ ) {bytestr.append (b) ؛} آخر {bytestr.append ("،" "،" " + b) ؛}} system.out.println (" ip: " + ipaddr +" byte-> byte []: [" + bytestr +"]) ؛ )) out.println ("byte []:" + bytestr + " -> IP:" + ipv4util.bytestoip (bytearr)) ؛ + " ->" + ipint) ؛ ipscope = ipv4util.getipintscope (ipandmask) ؛؛ + "-> قطاع عنوان IP: [ + ipv4util.inttoip (ipscope [0]) +" ، " + ipv4util.inttoip (ipscope [1])) ؛ string ipaddr1 =" 192.168.1.1 "، ipmask1 =" 255.255.055.0 "" ؛ "،" ipscope1 [1] + "]) ؛ system.out.println (ipaddr1 +" ، " + ipmask1 +" -> قطاع عنوان IP: [" + ipv4util.inttoip (ipscope1 [0]) +" ، " +" ipv4util.inttoip (ipscope1 [1]) + "]" ") ؛}}
من المأمول أن يكون هذا المقال مفيدًا لتصميم برنامج Java للجميع.