求IPV6函数

scvzhang 2007-07-01 09:32:59
1 IPV6检验函数 查看一个TEXT添的IPV6是否正确
2 IPV6 TO short[8]
3 IPV6 TO BYTE[16]
...全文
310 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
CSDN 好慢!!!花了一个多小时又重新上来了,呵呵~~
noOnlyCode 2007-07-03
  • 打赏
  • 举报
回复
说错,类库,汗一个……
noOnlyCode 2007-07-03
  • 打赏
  • 举报
回复
有函数库没?
  • 打赏
  • 举报
回复
::FFFF:d.d.d
::FFFF:d.d
::d.d.d
::d.d

上述地址是无效的,还有

::FFFF:d
::d

虽是有效的,但易与 IPv6 混淆,属于非常规的格式,在转换时一律看作是 IPv6 格式。

若为非正常的 IPv6 地址,在 toStandarIPv6() 方法中会产生异常,不知是否能够捕获异常来判断。

至于,正则表达式验证较为复杂,我暂时无法完成,肯请高人帮忙。

以下是更改后的代码段

public class IPv6 {

  public static void main(String[] args) {

    String ipv6 = "41:3::4:FFFF:210.147.158.1";    
    System.out.println(checkIPv6(ipv6));
    ipv6 = toStandarIPv6(ipv6);
    System.out.println(ipv6);
    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();
  }

  // 转换成标准格式的 IPv6
  public static String toStandarIPv6(String ipv6) {
    ipv6 = ipv6.trim();
    String[] ipv6s = ipv6.split(":");
    
    StringBuffer ip6 = new StringBuffer();
    for(int i=0; i<ipv6s.length; i++){
      if(ipv6s[i].length()!=0){
        while(ipv6s[i].length()<4){
          ipv6s[i] = "0" + ipv6s[i];
        }
      }
      ip6.append(ipv6s[i]).append(":");
    }
    ipv6 = ip6.toString().substring(0, ip6.length()-1);

    // 是否带有 IPv4 后缀
    int pre = (ipv6.indexOf('.') < 0) ? 8 : 7;
    
    // 补足 IPv6 中被压缩的部分
    if (ipv6s.length < pre) {
      StringBuffer sb = new StringBuffer();
      if (ipv6.indexOf("::") != 0) {
        sb.append(":");
      } else {
        sb.append("0000:");
      }
      for (int i = ipv6s.length; i <= pre; i++) {
        sb.append("0000:");
      }
      ipv6 = ipv6.replace("::", sb.toString());
    }
    
    if (pre == 7) {
      // 将后缀的 4 位 IPv4,转为 2 位的 IPv6
      String[] ipv4 = ipv6s[ipv6s.length - 1].split("\\.");
      StringBuffer sb = new StringBuffer();
      for (int i = 0; i < ipv4.length / 2; i++) {
        sb.append(":");
        String hex = Integer.toHexString(Integer.parseInt(ipv4[2 * i]));
        hex = (hex.length() < 2) ? "0" + hex : hex;
        sb.append(hex);
        hex = Integer.toHexString(Integer.parseInt(ipv4[2 * i + 1]));
        hex = (hex.length() < 2) ? "0" + hex : hex;
        sb.append(hex);
      }
      ipv6 = ipv6.substring(0, ipv6.lastIndexOf(":"));
      ipv6 = ipv6 + sb.toString();
    }
    return ipv6.toUpperCase();
  }

  // 将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 & 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;
  }

  public static boolean checkIPv6(String ipv6) {
    // 验证暂时无法完成,肯请高手帮忙
    return true;
  }
}

taotiede1 2007-07-03
  • 打赏
  • 举报
回复
学习一下
scvzhang 2007-07-03
  • 打赏
  • 举报
回复
bao110908(bao)(bao)
辛苦了。

学习中
rainv 2007-07-02
  • 打赏
  • 举报
回复
angelseyes 2007-07-02
  • 打赏
  • 举报
回复
学习,顶
ker79 2007-07-02
  • 打赏
  • 举报
回复
学习
  • 打赏
  • 举报
回复
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}");
  }
}
scvzhang 2007-07-02
  • 打赏
  • 举报
回复
bao110908(bao)(bao)
还有特殊的IPV6格式。下面是JDK里的IPV6事例,程序没有考虑。
1080:0:0:0:8:800:200C:417A
1080::8:800:200C:417A
::FFFF:129.144.52.38
::129.144.52.38
::FFFF:d.d.d
::FFFF:d.d
::d.d.d
::d.d
::FFFF:d

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧