`
codeall
  • 浏览: 24195 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

ip(58.246.0.0 - 58.247.255.255)到(58.246.0.0/15)的转换

 
阅读更多

对于恰好用一个掩码表示的段, 返回一个段, 例如
引用
58.246.0.0 - 58.247.255.255
->
引用
58.246.0.0/15

对于不能用一个掩码表示的端, 会返回一系列段. 例如
引用
0.0.0.0 - 0.0.5.128
->
引用
0.0.0.0/22;0.0.4.0/24;0.0.5.0/25;0.0.5.128/32;


	public static long ip2long(String strIP) {
		try {
			long[] ip = new long[4];
			int position1 = strIP.indexOf(".");
			int position2 = strIP.indexOf(".", position1 + 1);
			int position3 = strIP.indexOf(".", position2 + 1);

			ip[0] = Long.parseLong(strIP.substring(0, position1));
			ip[1] = Long.parseLong(strIP.substring(position1 + 1, position2));
			ip[2] = Long.parseLong(strIP.substring(position2 + 1, position3));
			ip[3] = Long.parseLong(strIP.substring(position3 + 1));
			return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3];
		} catch (Exception e) {
			return 0;
		}
	}

	public static String long2ip(long longIP) {
		return new StringBuffer(String.valueOf(longIP >>> 24)).append(".")
			.append(String.valueOf((longIP & 0x00FFFFFF) >>> 16)).append(".")
			.append(String.valueOf((longIP & 0x0000FFFF) >>> 8)).append(".")
			.append(String.valueOf(longIP & 0x000000FF)).toString();
	}

	public static Map<String, Integer> ipBlock2mask(String startIps, String endIps) {
		Map<String, Integer> blocks = new LinkedHashMap<String, Integer>();
		int mask = 32;
		long startIp = ip2long(startIps), endIp = ip2long(endIps) + 1;
		while (true) {
			if (startIp + new Double(Math.pow(2, 32 - mask)).longValue() > endIp) {
				// found the mask
				mask++;
				blocks.put(startIps, mask);
				long newStartIp = startIp + new Double(Math.pow(2, 32 - mask)).longValue();
				if (newStartIp >= endIp) {
					break; // exit all
				} else {
					startIp = newStartIp;
					startIps = long2ip(startIp);
					mask = 32;
				}
			}
			mask--;
		}
		return blocks;
	}
	
	// 0.0.0.0 - 0.0.5.128 => 0.0.0.0/22;0.0.4.0/24;0.0.5.0/25;0.0.5.128/32;
	// 0.0.0.0 - 0.0.5.255 => 0.0.0.0/22;0.0.4.0/23;
	public static String ipBlock2maskString(String startIps, String endIps) {
		Map<String, Integer> blocks = ipBlock2mask(startIps, endIps);
		StringBuilder sb = new StringBuilder();
		for (Map.Entry<String, Integer> entry : blocks.entrySet()) {
			sb.append(entry.getKey()).append("/").append(entry.getValue()).append(";");
		}
		return sb.toString();
	}
分享到:
评论

相关推荐

    新IP段58.30.0.0 58.31.255.255

    58.30.0.0 58.31.255.255 58.83.128.0 58.83.255.255 58.116.0.0 58.119.255.255 58.128.0.0 58.135.255.255 58.194.224.0 58.195.15.255 58.206.224.0 58.207.15.255 59.64.16.0 59.65.255.255 59.108.0.0 59.109....

    iso-iec 14496-10(3rd_2006-03-01)_MPEG4_AVC_H264.pdf

    Contents Page 0 Introduction.................................................................................................................................................................

    Debugging with GDB --2007年

    15 16 GDB Commands . . . . . . . . . . . . . . . . . . . . . . . . . 17 3.1 Command syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 3.2 Command completion . . . ....

    The Princeton Companion to Mathematics

    III.64 Optimization and Lagrange Multipliers 255 III.65 Orbifolds 257 III.66 Ordinals 258 III.67 The Peano Axioms 258 III.68 Permutation Groups 259 III.69 Phase Transitions 261 III.70 p 261 III.71 ...

    LTE from A-Z 培训教材 (part2 of 3)

    深入浅出,从基本概念到深入描述。是一份比较全面的教材。全文367页。有料有内容。是一份难得的教材。 文件比较大,所以拆成3部分。这是part2 LTE from A-Z Technology and Concepts of the 4G 3GPP Standard ...

    Doctrine ORM for PHP.pdf

    Doctrine Explained..................................................................................................15 Key Concepts........................................................................

    计算机网络第六版答案

    15. Google's private network connects together all its data centers, big and small. Traffic between the Google data centers passes over its private network rather than over the public Internet. Many ...

    LTE from A to Z (part1 of 3)

    深入浅出,从基本概念到深入描述。是一份比较全面的教材。全文367页。有料有内容。是一份难得的教材。 文件比较大,所以拆成3部分。这是part1 LTE from A-Z Technology and Concepts of the 4G 3GPP Standard ...

    LTE from A to Z

    深入浅出,从基本概念到深入描述。是一份比较全面的教材。全文367页。有料有内容。是一份难得的教材。 文件比较大,所以拆成3部分。这是part3 LTE from A-Z Technology and Concepts of the 4G 3GPP Standard ...

    spring-framework-reference4.1.4

    Not Using Commons Logging ................................................................... 12 Using SLF4J ..............................................................................................

    Tricks of the Windows video Game Programming---part1

    Initialization....................................................................17 Section 2: Enter Game Loop............................................................17 Section 3: Retrieve ...

    单片机接口技术实用子程序

    58 2.3 子程序8——基于RS-485的多机通信 .................................... 65 2 单片机接口技术实用子程序 2.3.1 RS-485接口标准 ...........................................................

    The Definitive Guide to NetBeans Platform

    ■CHAPTER 15 Extending the NetBeans IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 ■CHAPTER 16 From Eclipse RCP to the NetBeans Platform . . . . . . . . . . . . . . . . . ....

    3GPP-23501-g10(中文版).pdf

    前言 15 1 范围 16 2 参考文献 16 3 定义和缩写 20 3.1 定义 20 3.2 缩写 24 4 架构(architecture)模型和概念 26 4.1 一般概念 26 4.2 架构参考模型 27 4.2.1 概述 27 4.2.2 网络功能和实体 27 4.2.3 非漫游参考...

    Software Testing and Continuous Quality Improvement

    15 Software Quality Assurance Plan. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Steps to Develop and Implement a Software Quality Assurance Plan. . . . . . . . . . . . . . . ...

    spring-framework-reference-4.1.2

    Not Using Commons Logging ................................................................... 12 Using SLF4J ..............................................................................................

    图像处理(第二版)章毓晋

    ...................15 1.0.24 对一幅图像可使用多于一个线性操作符吗?............................................18 1.0.25 线性操作符使用的次序会导致结果的不同吗?..........................................

    C#语言参考(共285页)

    1.6 声明......................................15 1.7 类..........................................18 1.7.1 常数...............................20 1.7.2 域...................................20 1.7.3 方法...

    webx3框架指南PDF教程附学习Demo

    你可以使用全部的Webx,也可以仅仅使用到Webx的任何一个层次。 引言 ............................................................................................................................... ix 1. ...

Global site tag (gtag.js) - Google Analytics