inetaton java_INET_NTOA and INET_ATON in Java?

Using the IPAddress Java library it is simple, one line of code for each direction works for both IPv4 and IPv6. In fact, you can write code that works for both IPv4 and IPv6 as in the first example below.

Disclaimer: I am the project manager of that library.

IP-version agnostic using byte[] and/or BigInteger:

IPAddress loopback = new IPAddressString("::1").getAddress();

System.out.println(loopback.getValue());

IPAddress backAgain = new IPAddressGenerator().from(loopback.getBytes());

System.out.println(backAgain);

Use ints for IPv4:

IPv4Address loopbackv4 = new IPAddressString("127.0.0.1").getAddress().toIPv4();

System.out.println(loopbackv4.intValue());

IPv4Address backAgainv4 = new IPv4Address(loopbackv4.intValue());

System.out.println(backAgainv4);

Use BigInteger for IPv6:

IPv6Address loopbackv6 = new IPAddressString("::1").getAddress().toIPv6();

System.out.println(loopbackv6.getValue());

IPv6Address backAgainv6 = new IPv6Address(loopbackv6.getValue());

System.out.println(backAgainv6);

Output:

1

0:0:0:0:0:0:0:1

2130706433

127.0.0.1

1

0:0:0:0:0:0:0:1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值