IP地址转换成Long型数字

时间:2024-02-17 22:22:25
import org.junit.Test;

public class TransIpToLongNumber {
	public Long ipToNumber(String ip) {    
		Long ipLong = 0L; 
		String[] ipNumbers = ip.split("\\.");
		for (String ipNumber : ipNumbers) {
			ipLong = ipLong << 8 | Integer.parseInt(ipNumber);
		}
		return ipLong;   
	} 
	
	@Test
	public void trans(){
		System.out.println(ipToNumber("192.68.0.1"));
		System.out.println(ipToNumber("192.189.2.1"));
		System.out.println(ipToNumber("10.189.2.1"));
		System.out.println(ipToNumber("10.10.2.1"));
	}
	

}

参考