将点分式的IP地址转换成长整型
来源:互联网 发布:oa免费办公软件 编辑:程序博客网 时间:2024/05/17 02:59
/** * */package test;import java.net.InetAddress;import java.net.UnknownHostException;import java.nio.ByteBuffer;/** * @author wKF46214 * */public class IpConvert{ /** * @param args */ public static void main(String[] args) { try { long ip1 = convertIpFromString2Long("1.1.1.1"); long ip2 = convertIpFromString2Long("255.255.255.255"); System.out.println(ip1); System.out.println(ip2); long a = 3294967295L; String ip3 = convertIpFromLong2String(a); System.out.println(ip3); } catch (UnknownHostException e) { e.printStackTrace(); } } /** * 将点分式的IP地址转换成长整型 * @param ip * @return * @throws UnknownHostException */ public static long convertIpFromString2Long(String ip) throws UnknownHostException { if (ip == null || ip.equals("")) { return 0; } InetAddress ipaddr = InetAddress.getByName(ip); byte[] bas = ipaddr.getAddress(); int result = 0; result |= ((short) (bas[0] & 0x00ff) << 24); result |= ((short) (bas[1] & 0x00ff) << 16); result |= ((short) (bas[2] & 0x00ff) << 8); result |= (short) (bas[3] & 0x00ff); return convertUnsignedInt2Long(result); } public static long convertUnsignedInt2Long(int value) { long ret; ret = value >>> 1; ret <<= 1; ret |= (value << 31) >>> 31; return ret; } /** * 将长整型的IP地址转换成点分式 * * @param ip * @return * @throws UnknownHostException */ public static String convertIpFromLong2String(long ip) throws UnknownHostException { if (ip <= 0) { return ""; } ByteBuffer buf = ByteBuffer.allocate(4); // buf.order(ByteOrder.LITTLE_ENDIAN);//必须转换成小头序,才能正确显示IP buf.putInt(convertLong2UnsignedInt(ip)); InetAddress addr = InetAddress.getByAddress(buf.array()); return addr.getHostAddress(); } public static int convertLong2UnsignedInt(long value) { int ret; ret = (int) (value & 0x00000000ffffffff); return ret; }}
- 将点分式的IP地址转换成长整型
- Linux下网络IP地址的转换函数(点分式--长整型转换)
- python将IP地址转换成长整形的方法
- IP从整型地址转换为点分形式
- strtol() -- 将字符串转换成长整型数
- 将长整型的数转化为ip地址形式
- JAVA 长整型转换为IP地址的方法
- IP地址与整型数之间的转换
- 关于IP地址与整型之间的转换
- 将IP地址转成长整型函数
- 将整型ip转为点分十进制
- 如何转换IP地址为整型
- strtol(将字符串转换成长整型数)
- strtol() -- 将字符串转换成长整型数(转载)
- 如何将域名地址转换成对应的IP地址?
- 嵌入式 整型ip与点式ip互相转换
- IP地址转换为整型数字的PHP方法、Asp方法和MsSQL方法、MySQL方法
- ip 地址 整型和字符型互转换
- 获取某service是否在运行
- When Linux kernel panic, what can we do ?
- JAVA中观察者模式示例
- 代码签名证书FAQ
- MultiByteToWideChar和WideCharToMultiByte用法详解 .
- 将点分式的IP地址转换成长整型
- Adapter简介 SimpleAdapter
- C51资料收集汇总
- 第4章 ISE开发环境使用指南[FPGA开发实用教程]——第2节 HDL代码输入
- MapReduce编程模型的要点
- 如何注册公司
- 不用获得焦点也能实现跑马灯效果的textView
- Session实例:一次性验证码
- HDOJ 2049 不容易系列之(4)——考新郎