IPConvert.java类的主要作用是将ip(ipv6\ipv4)转换成16个字节的数组,其中ipv4的转换是先将v4地址转换成v6再转换成长度是16的字节数组
package com.cvicse.naba.common.utils; /** * * 描述: * <p> * ip转换byte数组管理类。 * </p> * 创建日期:2012-7-25 下午3:34:00<br> * * @author:Wendy<br> * @update:$Date: 2012-07-25 18:14:40 +0800 (Wed, 25 Jul 2012) $<br> * @version:$Revision: 779 $<br> * @since 1.0.0 */ public class IPConvert { private static final int IPV6Length = 8; // IPV6地址的分段 private static final int IPV4Length = 4; // IPV6地址分段 private static final int IPV4ParmLength = 2; // 一个IPV4分段占的长度 private static final int IPV6ParmLength = 4; // 一个IPV6分段占的长 /** * IPV6、IPV4转化为十六进制串 * * @param ipAddress * @return */ private static String buildKey(String ipAddress) { String Key = ""; // ipv4标识 。判断是否是ipv4地址 int dotFlag = ipAddress.indexOf("."); // ipv6标识 。判断是否是ipv6地址 int colonFlag = ipAddress.indexOf(":"); // ipv6标识 。判断是否是简写的ipv6地址 int dColonFlag = ipAddress.indexOf("::"); // 将v6或v4的分隔符用&代替 ipAddress = ipAddress.replace(".", "&"); ipAddress = ipAddress.replace(":", "&"); // ipv4 address。将ipv4地址转换成16进制的形式 if (dotFlag != -1 && colonFlag == -1) { String[] arr = ipAddress.split("&"); // 1、 ipv4转ipv6,前4组数补0或f for (int i = 0; i < IPV6Length - IPV4ParmLength; i++) { // 根据v4转v6的形式,除第4组数补ffff外,前3组数补0000 if (i == IPV6Length - IPV4ParmLength - 1) { Key += "ffff"; } else { Key += "0000"; } } // 2、将ipv4地址转成16进制 for (int j = 0; j < IPV4Length; j++) { // 1)将每组ipv4地址转换成16进制 arr[j] = Integer.toHexString(Integer.parseInt(arr[j])); // 2) 位数不足补0,ipv4地址中一组可转换成一个十六进制,两组数即可标识ipv6中的一组,v6中的一组数不足4位补0 for (int k = 0; k < (IPV4ParmLength - arr[j].length()); k++) { Key += "0"; } Key += arr[j]; } } // Mixed address with ipv4 and ipv6。将v4与v6的混合地址转换成16进制的形式 if (dotFlag != -1 && colonFlag != -1 && dColonFlag == -1) { String[] arr = ipAddress.split("&"); for (int i = 0; i < IPV6Length - IPV4ParmLength; i++) { // 将ip地址中每组不足4位的补0 for (int k = 0; k < (IPV6ParmLength - arr[i].length()); k++) { Key += "0"; } Key += arr[i]; } for (int j = 0; j < IPV4Length; j++) { arr[j] = Integer.toHexString(Integer.parseInt(arr[j])); for (int k = 0; k < (IPV4ParmLength - arr[j].length()); k++) { Key += "0"; } Key += arr[j]; } } // Mixed address with ipv4 and ipv6,and there are more than one // '0'。将v4与v6的混合地址(如::32:dc:192.168.62.174)转换成16进制的形式 // address param if (dColonFlag != -1 && dotFlag != -1) { String[] arr = ipAddress.split("&"); // 存放16进制的形式 String[] arrParams = new String[IPV6Length + IPV4ParmLength]; int indexFlag = 0; int pFlag = 0; // 1、将简写的ip地址补0 // 如果ip地址中前面部分采用简写,做如下处理 if ("".equals(arr[0])) { // 1)如果ip地址采用简写形式,不足位置补0,存放到arrParams中 for (int j = 0; j < (IPV6Length + IPV4ParmLength - (arr.length - 2)); j++) { arrParams[j] = "0000"; indexFlag++; } // 2)将已有值的部分(如32:dc:192.168.62.174)存放到arrParams中 for (int i = 2; i < arr.length; i++) { arrParams[indexFlag] = arr[i]; indexFlag++; } } else { for (int i = 0; i < arr.length; i++) { if ("".equals(arr[i])) { for (int j = 0; j < (IPV6Length + IPV4ParmLength - arr.length + 1); j++) { arrParams[indexFlag] = "0000"; indexFlag++; } } else { arrParams[indexFlag] = arr[i]; indexFlag++; } } } // 2、ip(去除ipv4的部分)中采用4位十六进制数表示一组数,将不足4位的十六进制数补0 for (int i = 0; i < IPV6Length - IPV4ParmLength; i++) { // 如果arrParams[i]组数据不足4位,前补0 for (int k = 0; k < (IPV6ParmLength - arrParams[i].length()); k++) { Key += "0"; } Key += arrParams[i]; // pFlag用于标识位置,主要用来标识ipv4地址的起始位 pFlag++; } // 3、将ipv4地址转成16进制 for (int j = 0; j < IPV4Length; j++) { // 1)将每组ipv4地址转换成16进制 arrParams[pFlag] = Integer.toHexString(Integer .parseInt(arrParams[pFlag])); // 2)位数不足补0,ipv4地址中一组可转换成一个十六进制,两组数即可标识ipv6中的一组,v6中的一组数不足4位补0 for (int k = 0; k < (IPV4ParmLength - arrParams[pFlag].length()); k++) { Key += "0"; } Key += arrParams[pFlag]; pFlag++; } } // ipv6 address。将ipv6地址转换成16进制 if (dColonFlag == -1 && dotFlag == -1 && colonFlag != -1) { String[] arrParams = ipAddress.split("&"); // 将v6地址转成十六进制 for (int i = 0; i < IPV6Length; i++) { // 将ipv6地址中每组不足4位的补0 for (int k = 0; k < (IPV6ParmLength - arrParams[i].length()); k++) { Key += "0"; } Key += arrParams[i]; } } if (dColonFlag != -1 && dotFlag == -1) { String[] arr = ipAddress.split("&"); String[] arrParams = new String[IPV6Length]; int indexFlag = 0; if ("".equals(arr[0])) { for (int j = 0; j < (IPV6Length - (arr.length - 2)); j++) { arrParams[j] = "0000"; indexFlag++; } for (int i = 2; i < arr.length; i++) { arrParams[indexFlag] = arr[i]; i++; indexFlag++; } } else { for (int i = 0; i < arr.length; i++) { if ("".equals(arr[i])) { for (int j = 0; j < (IPV6Length - arr.length + 1); j++) { arrParams[indexFlag] = "0000"; indexFlag++; } } else { arrParams[indexFlag] = arr[i]; indexFlag++; } } } for (int i = 0; i < IPV6Length; i++) { for (int k = 0; k < (IPV6ParmLength - arrParams[i].length()); k++) { Key += "0"; } Key += arrParams[i]; } } return Key; } /** * 十六进制串转化为IP地址 * * @param key * @return */ private static String splitKey(String key) { String IPV6Address = ""; String IPAddress = ""; String strKey = ""; String ip1 = key.substring(0, 24); String tIP1 = ip1.replace("0000", "").trim(); if (!"".equals(tIP1) && !"FFFF".equals(tIP1)) { // 将ip按:分隔 while (!"".equals(key)) { strKey = key.substring(0, 4); key = key.substring(4); if ("".equals(IPV6Address)) { IPV6Address = strKey; } else { IPV6Address += ":" + strKey; } } IPAddress = IPV6Address; } return IPAddress; } /** * 将ip地址都转成16个字节的数组。先将v6地址存以":"分隔存放到数组中,再将数组中的每两位取存到长度为16的字符串数组中, * 再将这两位十六进制数转成十进制,再转成byte类型存放到16个字的数组中。 * * @param ip * @return */ public static byte[] toByte(String ip) { // 将ip地址转换成16进制 String Key = buildKey(ip); // 将16进制转换成ip地址 String ip6 = splitKey(Key); // 将v6f地址存以":"分隔存放到数组中 String[] ip6Str = ip6.split(":"); String[] ipStr = new String[16]; byte[] ip6Byte = new byte[16]; // 将数组中的每两位取存到长度为16的字符串数组中 for (int j = 0, i = 0; i < ip6Str.length; j = j + 2, i++) { ipStr[j] = ip6Str[i].substring(0, 2); ipStr[j + 1] = ip6Str[i].substring(2, 4); } // 将ipStr中的十六进制数转成十进制,再转成byte类型存放到16个字的数组中 for (int i = 0; i < ip6Byte.length; i++) { ip6Byte[i] = (byte) Integer.parseInt(ipStr[i], 16); } return ip6Byte; } }
相关推荐
IPV4-IPV6过渡转换技术设计与实现IPV4-IPV6过渡转换技术设计与实现IPV4-IPV6过渡转换技术设计与实现IPV4-IPV6过渡转换技术设计与实现
IPv4_IPv6转换网关的设计与实现 论文
IPV4与IPV6兼容socket编程接口详解[归纳].pdf
IPV4-IPV6过渡转换技术设计与实现IPV4-IPV6过渡转换技术设计与实现1
IPV4/IPV6过渡技术的研究与设计,IPV4/IPV6过渡技术的研究与设计,IPV4/IPV6过渡技术的研究与设计
批量ipv4/ipv6地址生成小工具
随着网络体系结构的演变和宽带技术的发展,传统网络向 下一代网络的演进势不可挡。...传统网络中实现下一代网络应用的核心技术,IPv4/IPv6过渡 技术是用来在IPv4向IPv6演进的过渡期内,保证业务共存和互 操作
对IPV4转换IPv6技术做了全面的论述,我喜欢叫它IPv6白皮书。因为它关于转换技术方面问题论述的太全面精辟了
文档及源码都是本人自己所写或修改的,修改后的ntpclient程序目前在实际项目中使用(其实很简单). IPV4和IPV6兼容的socket编程主要有以下内容: ...4. IPV6服务器端与客户端 5 实例支持IPV6的ntpclient源码
ipv4与ipv6 socket兼容编程的方法。有基于TCP与UDP的不同条件。
本文件讨论从IPv6地址到相应IPv4地址(或者相反)的算法转换,仅使用静态配置信息。本文件定义算法转换中使用的熟知前缀...算法转换在IPv4/IPv6转换器中使用,也由其他类型代理和网关(例如,DNS)在IPv4/IPv6场景中应用。
对IPv4向IPv6转换技术做了详细的论述,我爱叫它IPv6白皮书,它对这一过渡阶段的问题很彻底。感觉很实用。
IPV4-IPV6过渡转换技术设计与实现3IPV4-IPV6过渡转换技术设计与实现3IPV4-IPV6过渡转换技术设计与实现
IPV4和IPV6的区别 IPV4和IPV6的地址长度
IPV4-IPV6过渡转换技术设计与实现2IPV4-IPV6过渡转换技术设计与实现2IPV4-IPV6过渡转换技术设计与实现
一个小论文,从不同的方向将IPV4与IPV6做了简单的比较。
IPv4与IPv6业务-FTP和TFTP技术介绍.pdf
ipv4或ipv6字符串形式的地址转换为BigInteger数字。
使用C++在Linux下编写的TCP client与server连接的一个很简单的例子程序,同时支持IPV6与IPV4,适合相关人员参考。
IPv4/IPv6转换网关的数据模型研究,张伟建,张雪洁,在IPv4网络向Ipv6网络过渡过程中,转化网关因其在配置过程中无损已有的网络环境成为目前甚至是相当长一段时期内的最优选择。本文对�