IPV6检验函数,查看一个TEXT添的IPV6是否正确,火龙果的IPV6验证
火龙果的IPV6验证public class IPv6 { public static void main(String[] args) { String ipv6 = "3255:0304:0000:FE4A:174F:5577:289C:0014 "; short[] s = ipv6ToShort(ipv6); byte[] b = ipv6ToByte(ipv6); for (short c : s) { System.out.printf( "%9d ", c); } System.out.println(); for (byte c : b) { System.out.printf( "%4d ", c); } System.out.println(); System.out.println(checkIPv6(ipv6)); } // 将 IPv6 转为 byte,大于 0x7F 的值用负数表示 public static byte[] ipv6ToByte(String ipv6) { byte[] ipv6Byte = new byte; String[] ipv6s = ipv6.split( ": "); for (int i = 0; i < ipv6Byte.length / 2; i++) { int k = Integer.parseInt(ipv6s, 16); ipv6Byte = (byte) (k > > 8); ipv6Byte = (byte) ((k | 0xff00) & 0xff); } return ipv6Byte; } // 将 IPv6 转为 short,大于 0x7FFF 的值用负数表示 public static short[] ipv6ToShort(String ipv6) { short[] ipv6Short = new short; String[] ipv6s = ipv6.split( ": "); for (int i = 0; i < ipv6Short.length; i++) { ipv6Short = (short)Integer.parseInt(ipv6s, 16); } return ipv6Short; } // 检验 IPv6 的格式 public static boolean checkIPv6(String ipv6) { return ipv6.matches( "(\\p{XDigit}){1,4}(:(\\p{XDigit}){1,4}){7} "); } }
黑色头发 http://heisetoufa.iteye.com
页:
[1]