将压缩的 IPv6 地址转换为其长格式的代码

问题描述 投票:0回答:2

我想知道是否有一个库可以用来将压缩的 IPv6 地址(例如 0:9876:5700::9)的表示形式转换为长 IPv6 形式(在本例中:

0000:9876:5700:0000:0000:0000:0000:0009).

java ip-address ipv6
2个回答
1
投票

此转换将进行转换:

ip = ip. replaceAll("^", "0000").replaceAll("::", "0000:0000:0000:0000:0000").replaceAll("^.*?(....)(?=:)|(?<=:)[^:]*(?=....$)", "");

末尾额外的

replaceAll()
确保第一个/最后部分中没有多余的零,从而满足前导/尾随地址信息(在本例中为“0”和“9”)为任意位数的需求。


0
投票
import com.google.common.net.InetAddresses;
// Convert the short IPv6 address to a full version
InetAddress inetAddress = InetAddresses.forString(shortIPv6); 
return inetAddress.getHostAddress();


import java.net.Inet6Address;
import java.net.InetAddress;
InetAddress inetAddress = Inet6Address.getByName(shortIPv6); 
return inetAddress.getHostAddress();
© www.soinside.com 2019 - 2024. All rights reserved.