Conversion IPv6 to long and long to IPv6

前端 未结 4 1475
小蘑菇
小蘑菇 2020-12-15 06:53

How should I perform conversion from IPv6 to long and vice versa?

So far I have:

    public static long IPTo         


        
4条回答
  •  情话喂你
    2020-12-15 07:05

    An IPv6 address can not be stored in long. You can use BigInteger instead of long.

    public static BigInteger ipv6ToNumber(String addr) {
        int startIndex=addr.indexOf("::");
    
        if(startIndex!=-1){
    
    
            String firstStr=addr.substring(0,startIndex);
            String secondStr=addr.substring(startIndex+2, addr.length());
    
    
            BigInteger first=ipv6ToNumber(firstStr);
    
            int x=countChar(addr, ':');
    
            first=first.shiftLeft(16*(7-x)).add(ipv6ToNumber(secondStr));
    
            return first;
        }
    
    
        String[] strArr = addr.split(":");
    
        BigInteger retValue = BigInteger.valueOf(0);
        for (int i=0;i

提交回复
热议问题