表示 IPV6 地址的缩写形式

发布于 2024-11-27 23:35:14 字数 124 浏览 0 评论 0原文

我有一个 IPv6 地址字符串: 2001:1:0:0:10:0:10:10

我想将其表示为 IPV6 字符串的缩写形式: 2001:1::10:0:10:10

有没有人知道java方法来做到这一点吗?

I have an IPv6 address string: 2001:1:0:0:10:0:10:10

I want to represent it as a short form of IPV6 string: 2001:1::10:0:10:10

Does any one know the java methods to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离旧人 2024-12-04 23:35:14

由于在某些情况下它可以通过多种不同的方式缩短,因此java API中可能没有这样的函数。你可以手动做:

Inet6Address.getByName("1080::8:800:200C:417A").replaceFirst("(:0)+:", "::");

但我没有测试得很好。在某些情况下此代码可能是错误的。

Since it can be shorten in many different ways in some cases, there is probably no such function in java API. You can manually do:

Inet6Address.getByName("1080::8:800:200C:417A").replaceFirst("(:0)+:", "::");

but I did'n test it very well. There might be some cases this code is wrong.

谢绝鈎搭 2024-12-04 23:35:14

开源 IPAddress Java 库 可以提供多种生成 IPv4 和/或 IPv6 字符串的方法,包括与 rfc 5952 匹配的 IPv6 规范字符串。免责声明:我是该库的项目经理。

toCanonicalString() 方法生成规范字符串,还有一个稍有不同的 toCompressedString() 方法。对于规范字符串,单个零段不会被压缩,但 toCompressedString() 将压缩这样的段。 toNormalizedString() 方法根本不会压缩。

使用您的示例 2001:1:0:0:10:0:10:10 和另一个示例代码:

IPAddress addr = new IPAddressString("2001:1:0:0:10:0:10:10").getAddress();
System.out.println(addr.toNormalizedString());
System.out.println(addr.toCanonicalString());
System.out.println(addr.toCompressedString());
System.out.println();

addr = new IPAddressString("2001:db8:0:1:1:1:1:1").getAddress();
System.out.println(addr.toNormalizedString());
System.out.println(addr.toCanonicalString());
System.out.println(addr.toCompressedString());

输出:

2001:1:0:0:10:0:10:10
2001:1::10:0:10:10
2001:1::10:0:10:10

2001:db8:0:1:1:1:1:1
2001:db8:0:1:1:1:1:1
2001:db8::1:1:1:1:1

The open-source IPAddress Java library can provides numerous ways of producing strings for IPv4 and/or IPv6, including the canonical string for IPv6 matching rfc 5952. Disclaimer: I am the project manager of that library.

The method toCanonicalString() produces the canonical string, there is also a method toCompressedString() that is slightly different. With the canonical string a single segment of zero is not compressed, but toCompressedString() will compress such a segment. The method toNormalizedString() will not compress at all.

Using your example 2001:1:0:0:10:0:10:10 and another here is sample code:

IPAddress addr = new IPAddressString("2001:1:0:0:10:0:10:10").getAddress();
System.out.println(addr.toNormalizedString());
System.out.println(addr.toCanonicalString());
System.out.println(addr.toCompressedString());
System.out.println();

addr = new IPAddressString("2001:db8:0:1:1:1:1:1").getAddress();
System.out.println(addr.toNormalizedString());
System.out.println(addr.toCanonicalString());
System.out.println(addr.toCompressedString());

Output:

2001:1:0:0:10:0:10:10
2001:1::10:0:10:10
2001:1::10:0:10:10

2001:db8:0:1:1:1:1:1
2001:db8:0:1:1:1:1:1
2001:db8::1:1:1:1:1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文