首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Java IP 地址與整數互轉工具類

     編輯:Java 相關知識

Java IP 地址與整數互轉工具類。本站提醒廣大學習愛好者:此文章僅供參考,不一定能完全符合您的需求。以下是完整工具類正文


本文範例說明 Java 中 IP 地址與整數互相轉換的方法。分享給大家供參考。詳細分析如下:

1、基礎知識點

IP → 整數:
將 IP 地址轉換為位元組陣列
透過左移位(<<)、與(&)、或(|)操作轉為 int

整數 → IP:
將整數值進行無符號右移位操作(>>>),右移 24 位,再與 0xFF 運算,得到第一段 IP。
右移 16 位,與 0xFF,得到第二段 IP。
右移 8 位,與 0xFF,得到第三段 IP。
直接與 0xFF 運算,得到第四段 IP。

2、Java 程式碼範例(IPv4Util.java)

package michael.utils;
import java.net.InetAddress;

public class IPv4Util {
    private final static int INADDRSZ = 4;

    public static byte[] ipToBytesByInet(String ipAddr) {
        try {
            return InetAddress.getByName(ipAddr).getAddress();
        } catch (Exception e) {
            throw new IllegalArgumentException(ipAddr + " is invalid IP");
        }
    }

    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");
        }
    }

    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();
    }

    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;
    }

    public static int ipToInt(String ipAddr) {
        try {
            return bytesToInt(ipToBytesByInet(ipAddr));
        } catch (Exception e) {
            throw new IllegalArgumentException(ipAddr + " is invalid IP");
        }
    }

    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;
    }

    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();
    }

    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 };
    }

    public static String[] getIPAddrScope(String ipAndMask) {
        int[] ipIntArr = IPv4Util.getIPIntScope(ipAndMask);
        return new String[] { IPv4Util.intToIp(ipIntArr[0]), IPv4Util.intToIp(ipIntArr[0]) };
    }

    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);
        }
    }

    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]) };
    }

    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]) + " ]");
    }
}

希望本文所述對大家的 Java 程式設計有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved