Conversion IPv6 to long and long to IPv6

前端 未结 4 1476
小蘑菇
小蘑菇 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条回答
  •  猫巷女王i
    2020-12-15 07:21

    An IPv6 address is a 128-bit number as described here. A long in Java is represented on 64 bits, so you need another structure, like a BigDecimal or two longs (a container with an array of two longs or simply an array of two longs) in order to store an IPv6 address.

    Below is an example (just to provide you an idea):

    public class Asd {
    
    public static long[] IPToLong(String addr) {
        String[] addrArray = addr.split(":");//a IPv6 adress is of form 2607:f0d0:1002:0051:0000:0000:0000:0004
        long[] num = new long[addrArray.length];
    
        for (int i=0; i> 16;
            }
        }
        return ipString;
    
    }
    
    static public void main(String[] args) {
        String ipString = "2607:f0d0:1002:0051:0000:0000:0000:0004";
        long[] asd = IPToLong(ipString);
    
        System.out.println(longToIP(asd));
    }
    

    }

提交回复
热议问题