<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 241,  comments - 116,  trackbacks - 0
    一、基本知識點

     

    IP ——> 整數:

    • 把IP地址轉化為字節數組
    • 通過左移位(<<)、與(&)、或(|)這些操作轉為int

    整數 ——> IP:

    • 將整數值進行右移位操作(>>>),右移24位,再進行與操作符(&)0xFF,得到的數字即為第一段IP。
    • 將整數值進行右移位操作(>>>),右移16位,再進行與操作符(&)0xFF,得到的數字即為第二段IP。
    • 將整數值進行右移位操作(>>>),右移8位,再進行與操作符(&)0xFF,得到的數字即為第三段IP。
    • 將整數值進行與操作符(&)0xFF,得到的數字即為第四段IP。

    二、java代碼示例

        IPv4Util.java
    package michael.utils;

    import java.net.InetAddress;

    /**
     * @author michael <br>
     *         blog: http://sjsky.iteye.com <br>
     *         mail: sjsky007@gmail.com
     */
    public class IPv4Util {

        private final static int INADDRSZ = 4;

        /**
         * 把IP地址轉化為字節數組
         * @param ipAddr
         * @return byte[]
         */
        public static byte[] ipToBytesByInet(String ipAddr) {
            try {
                return InetAddress.getByName(ipAddr).getAddress();
            } catch (Exception e) {
                throw new IllegalArgumentException(ipAddr + " is invalid IP");
            }
        }JTA實踐:Spring+ATOMIKOS

        /**
         * 把IP地址轉化為int
         * @param ipAddr
         * @return int
         */
        public static byte[] ipToBytesByReg(String ipAddr) {
            byte[] ret = new byte[4];
            try {
                String[] ipArr = ipAddr.split("\\.");
                ret[0] = (byte) (Integer.parseInt(ipArr[0]) & 0xFF);
                ret[1] = (byte) (Integer.parseInt(ipArr[1]) & 0xFF);
                ret[2] = (byte) (Integer.parseInt(ipArr[2]) & 0xFF);
                ret[3] = (byte) (Integer.parseInt(ipArr[3]) & 0xFF);
                return ret;
            } catch (Exception e) {
                throw new IllegalArgumentException(ipAddr + " is invalid IP");
            }

        }

        /**
         * 字節數組轉化為IP
         * @param bytes
         * @return int
         */
        public static String bytesToIp(byte[] bytes) {
            return new StringBuffer().append(bytes[0] & 0xFF).append('.').append(
                    bytes[1] & 0xFF).append('.').append(bytes[2] & 0xFF)
                    .append('.').append(bytes[3] & 0xFF).toString();
        }

        /**
         * 根據位運算把 byte[] -> int
         * @param bytes
         * @return int
         */
        public static int bytesToInt(byte[] bytes) {
            int addr = bytes[3] & 0xFF;
            addr |= ((bytes[2] << 8) & 0xFF00);
            addr |= ((bytes[1] << 16) & 0xFF0000);
            addr |= ((bytes[0] << 24) & 0xFF000000);
            return addr;
        }

        /**
         * 把IP地址轉化為int
         * @param ipAddr
         * @return int
         */
        public static int ipToInt(String ipAddr) {
            try {
                return bytesToInt(ipToBytesByInet(ipAddr));
            } catch (Exception e) {
                throw new IllegalArgumentException(ipAddr + " is invalid IP");
            }
        }

        /**
         * ipInt -> byte[]
         * @param ipInt
         * @return byte[]
         */
        public static byte[] intToBytes(int ipInt) {
            byte[] ipAddr = new byte[INADDRSZ];
            ipAddr[0] = (byte) ((ipInt >>> 24) & 0xFF);
            ipAddr[1] = (byte) ((ipInt >>> 16) & 0xFF);
            ipAddr[2] = (byte) ((ipInt >>> 8) & 0xFF);
            ipAddr[3] = (byte) (ipInt & 0xFF);
            return ipAddr;
        }

        /**
         * 把int->ip地址
         * @param ipInt
         * @return String
         */
        public static String intToIp(int ipInt) {
            return new StringBuilder().append(((ipInt >> 24) & 0xff)).append('.')
                    .append((ipInt >> 16) & 0xff).append('.').append(
                            (ipInt >> 8) & 0xff).append('.').append((ipInt & 0xff))
                    .toString();
        }

        /**
         * 把192.168.1.1/24 轉化為int數組范圍
         * @param ipAndMask
         * @return int[]
         */
        public static int[] getIPIntScope(String ipAndMask) {

            String[] ipArr = ipAndMask.split("/");
            if (ipArr.length != 2) {
                throw new IllegalArgumentException("invalid ipAndMask with: "
                        + ipAndMask);
            }
            int netMask = Integer.valueOf(ipArr[1].trim());
            if (netMask < 0 || netMask > 31) {
                throw new IllegalArgumentException("invalid ipAndMask with: "
                        + ipAndMask);
            }
            int ipInt = IPv4Util.ipToInt(ipArr[0]);
            int netIP = ipInt & (0xFFFFFFFF << (32 - netMask));
            int hostScope = (0xFFFFFFFF >>> netMask);
            return new int[] { netIP, netIP + hostScope };

        }

        /**
         * 把192.168.1.1/24 轉化為IP數組范圍
         * @param ipAndMask
         * @return String[]
         */
        public static String[] getIPAddrScope(String ipAndMask) {
            int[] ipIntArr = IPv4Util.getIPIntScope(ipAndMask);
            return new String[] { IPv4Util.intToIp(ipIntArr[0]),
                    IPv4Util.intToIp(ipIntArr[0]) };
        }

        /**
         * 根據IP 子網掩碼(192.168.1.1 255.255.255.0)轉化為IP段
         * @param ipAddr ipAddr
         * @param mask mask
         * @return int[]
         */
        public static int[] getIPIntScope(String ipAddr, String mask) {

            int ipInt;
            int netMaskInt = 0, ipcount = 0;
            try {
                ipInt = IPv4Util.ipToInt(ipAddr);
                if (null == mask || "".equals(mask)) {
                    return new int[] { ipInt, ipInt };
                }
                netMaskInt = IPv4Util.ipToInt(mask);
                ipcount = IPv4Util.ipToInt("255.255.255.255") - netMaskInt;
                int netIP = ipInt & netMaskInt;
                int hostScope = netIP + ipcount;
                return new int[] { netIP, hostScope };
            } catch (Exception e) {
                throw new IllegalArgumentException("invalid ip scope express  ip:"
                        + ipAddr + "  mask:" + mask);
            }

        }

        /**
         * 根據IP 子網掩碼(192.168.1.1 255.255.255.0)轉化為IP段
         * @param ipAddr ipAddr
         * @param mask mask
         * @return String[]
         */
        public static String[] getIPStrScope(String ipAddr, String mask) {
            int[] ipIntArr = IPv4Util.getIPIntScope(ipAddr, mask);
            return new String[] { IPv4Util.intToIp(ipIntArr[0]),
                    IPv4Util.intToIp(ipIntArr[0]) };
        }

        /**
         * @param args
         * @throws Exception
         */
        public static void main(String[] args) throws Exception {
            String ipAddr = "192.168.8.1";

            byte[] bytearr = IPv4Util.ipToBytesByInet(ipAddr);

            StringBuffer byteStr = new StringBuffer();

            for (byte b : bytearr) {
                if (byteStr.length() == 0) {
                    byteStr.append(b);
                } else {
                    byteStr.append("," + b);
                }
            }
            System.out.println("IP: " + ipAddr + " ByInet --> byte[]: [ " + byteStr
                    + " ]");

            bytearr = IPv4Util.ipToBytesByReg(ipAddr);
            byteStr = new StringBuffer();

            for (byte b : bytearr) {
                if (byteStr.length() == 0) {
                    byteStr.append(b);
                } else {
                    byteStr.append("," + b);
                }
            }
            System.out.println("IP: " + ipAddr + " ByReg  --> byte[]: [ " + byteStr
                    + " ]");

            System.out.println("byte[]: " + byteStr + " --> IP: "
                    + IPv4Util.bytesToIp(bytearr));

            int ipInt = IPv4Util.ipToInt(ipAddr);

            System.out.println("IP: " + ipAddr + "  --> int: " + ipInt);

            System.out.println("int: " + ipInt + " --> IP: "
                    + IPv4Util.intToIp(ipInt));

            String ipAndMask = "192.168.1.1/24";

            int[] ipscope = IPv4Util.getIPIntScope(ipAndMask);
            System.out.println(ipAndMask + " --> int地址段:[ " + ipscope[0] + ","
                    + ipscope[1] + " ]");

            System.out.println(ipAndMask + " --> IP 地址段:[ "
                    + IPv4Util.intToIp(ipscope[0]) + ","
                    + IPv4Util.intToIp(ipscope[1]) + " ]");

            String ipAddr1 = "192.168.1.1", ipMask1 = "255.255.255.0";

            int[] ipscope1 = IPv4Util.getIPIntScope(ipAddr1, ipMask1);
            System.out.println(ipAddr1 + " , " + ipMask1 + "  --> int地址段 :[ "
                    + ipscope1[0] + "," + ipscope1[1] + " ]");

            System.out.println(ipAddr1 + " , " + ipMask1 + "  --> IP地址段 :[ "
                    + IPv4Util.intToIp(ipscope1[0]) + ","
                    + IPv4Util.intToIp(ipscope1[1]) + " ]");

        }
    }
    posted on 2011-07-08 10:48 墻頭草 閱讀(13497) 評論(1)  編輯  收藏

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    人人游戲網 軟件開發網 貨運專家
    主站蜘蛛池模板: 日韩精品成人无码专区免费| 国产精品免费看久久久 | 永久免费无码网站在线观看| 亚洲色欲或者高潮影院| 久久爰www免费人成| 久久91亚洲精品中文字幕| 久久青草91免费观看| 久久精品国产亚洲av高清漫画 | 16女性下面扒开无遮挡免费| 久久久亚洲欧洲日产国码二区| 久久国产乱子免费精品| 亚洲国产精品久久66| 最近最新高清免费中文字幕 | 亚洲?V乱码久久精品蜜桃 | 亚洲一二成人精品区| 最近中文字幕mv免费高清在线 | 国产视频精品免费视频| 浮力影院亚洲国产第一页| 3344在线看片免费| 精品亚洲成AV人在线观看| 三年片在线观看免费大全| 最新亚洲人成无码网站| 国产亚洲精品久久久久秋霞| 日韩精品内射视频免费观看| 亚洲欧洲日韩极速播放| 一本色道久久88亚洲综合| 在线观看片免费人成视频无码| 亚洲神级电影国语版| 国产无遮挡裸体免费视频| 中文字幕免费在线视频| 亚洲不卡中文字幕| 亚洲AV无码一区二区三区在线观看| 中文字幕免费观看全部电影| 亚洲六月丁香六月婷婷蜜芽| 免费欧洲美女牲交视频| 香蕉成人免费看片视频app下载| 精品日韩99亚洲的在线发布 | 亚洲AV永久无码精品一百度影院| 91高清免费国产自产| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 黄色片在线免费观看|