2025-05-23

在項目中經常會用到IP(v4)范圍判定比較的功能,一般將IP轉化為整數再進行比較。
一、基本知識點



 


IP ——> 整數:


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


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


    IPv4Util.java



 


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”);  
        }  
    }  
 
    /** 
     * 把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(  
                  &n

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *