求IPV6函数
1 IPV6检验函数 查看一个TEXT添的IPV6是否正确
2 IPV6 TO short[8]
3 IPV6 TO BYTE[16]
------解决方案--------------------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[16],大于 0x7F 的值用负数表示
public static byte[] ipv6ToByte(String ipv6) {
byte[] ipv6Byte = new byte[16];
String[] ipv6s = ipv6.split( ": ");
for (int i = 0; i < ipv6Byte.length / 2; i++) {
int k = Integer.parseInt(ipv6s[i], 16);
ipv6Byte[2 * i] = (byte) (k > > 8);
ipv6Byte[2 * i + 1] = (byte) ((k | 0xff00) & 0xff);
}
return ipv6Byte;
}
// 将 IPv6 转为 short[8],大于 0x7FFF 的值用负数表示
public static short[] ipv6ToShort(String ipv6) {
short[] ipv6Short = new short[8];
String[] ipv6s = ipv6.split( ": ");
for (int i = 0; i < ipv6Short.length; i++) {
ipv6Short[i] = (short)Integer.parseInt(ipv6s[i], 16);
}
return ipv6Short;
}
// 检验 IPv6 的格式
public static boolean checkIPv6(String ipv6) {
return ipv6.matches( "(\\p{XDigit}){1,4}(:(\\p{XDigit}){1,4}){7} ");
}
}
------解决方案--------------------学习
------解决方案--------------------学习,顶
------解决方案--------------------顶
------解决方案--------------------有函数库没?
------解决方案--------------------学习一下