Conversion IPv6 to long and long to IPv6

前端 未结 4 1457
小蘑菇
小蘑菇 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:17

    You can also use java.net.InetAddress
    It works with both ipv4 and ipv6 (all formats)

    public static BigInteger ipToBigInteger(String addr) {
        InetAddress a = InetAddress.getByName(addr)
        byte[] bytes = a.getAddress()
        return new BigInteger(1, bytes)
    }
    

提交回复
热议问题