Java 如何将 IPV6 地址转换为 IPV4 地址?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2786632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I convert IPV6 address to IPV4 address?
提问by newbie
I have application that uses IPv4
addresses (it stores them as long), so it only understands IPv4
addresses.
我有使用IPv4
地址的应用程序(它存储它们很长时间),所以它只理解IPv4
地址。
Is it possible to convert IPv6
address to IPv4
with Java
?
是否可以将IPv6
地址转换为IPv4
with Java
?
采纳答案by AllenJB
While there are IPv6 equivalents for the IPv4 address range, you can't convert all IPv6 addresses to IPv4 - there are more IPv6 addresses than there are IPv4 addresses.
尽管IPv4 地址范围有 IPv6 等效项,但您无法将所有 IPv6 地址转换为 IPv4 - IPv6 地址比 IPv4 地址多。
The only sane way around this issue is to update your application to be able to understand and store IPv6 addresses.
解决此问题的唯一明智方法是更新您的应用程序,使其能够理解和存储 IPv6 地址。
回答by Damien_The_Unbeliever
There isn't a 1-1 correspondence between IPv4 and IPv6 addresses (nor between IP addresses and devices), so what you're asking for generally isn't possible.
IPv4 和 IPv6 地址之间(也不在 IP 地址和设备之间)之间没有 1-1 对应关系,因此您所要求的通常是不可能的。
There is a particular range of IPv6 addresses that actually represent the IPv4 address space, but general IPv6 addresses will not be from this range.
有一个特定范围的 IPv6 地址实际上代表了 IPv4 地址空间,但一般的 IPv6 地址不会来自这个范围。
回答by aioobe
Some googling led me to the following post:
一些谷歌搜索使我找到了以下帖子:
http://www.developerweb.net/forum/showthread.php?t=3434
http://www.developerweb.net/forum/showthread.php?t=3434
The code provided in the post is in C, but it shouldn't be too hard to rewrite it to Java.
帖子中提供的代码是用 C 编写的,但是将其重写为 Java 应该不会太难。
回答by Vishnuraj V
Here is the code you are looking for in javascript. Well you know you can't convert all of the ipv6 addresses
这是您在 javascript 中寻找的代码。你知道你不能转换所有的 ipv6 地址
<script>
function parseIp6(str)
{
//init
var ar=new Array;
for(var i=0;i<8;i++)ar[i]=0;
//check for trivial IPs
if(str=="::")return ar;
//parse
var sar=str.split(':');
var slen=sar.length;
if(slen>8)slen=8;
var j=0;
for(var i=0;i<slen;i++){
//this is a "::", switch to end-run mode
if(i && sar[i]==""){j=9-slen+i;continue;}
ar[j]=parseInt("0x0"+sar[i]);
j++;
}
return ar;
}
function ipcnvfrom6(ip6)
{
var ip6=parseIp6(ip6);
var ip4=(ip6[6]>>8)+"."+(ip6[6]&0xff)+"."+(ip6[7]>>8)+"."+(ip6[7]&0xff);
return ip4;
}
alert(ipcnvfrom6("::C0A8:4A07"));
</script>
回答by Sean F
The IPAddress Java librarycan accomplish what you are describing here.
IPAddress Java 库可以完成您在此处描述的内容。
IPv6 addresses are 16 bytes. Using that library, if you are starting with a 16-byte array you can construct the IPv6 address object:
IPv6 地址为 16 个字节。使用该库,如果您从 16 字节数组开始,您可以构造 IPv6 地址对象:
IPv6Address addr = new IPv6Address(bytes); //bytes is byte[16]
From there you can check if the address is IPv4 mapped, IPv4 compatible, IPv4 translated, and so on (there are many possible ways IPv6 represents IPv4 addresses). In most cases, if an IPv6 address represents an IPv4 address, the ipv4 address is in the lower 4 bytes, and so you can get the derived IPv4 address as follows. Afterwards, you can convert back to bytes, which will be just 4 bytes for IPv4.
从那里您可以检查地址是否已映射 IPv4、IPv4 兼容、IPv4 转换等(IPv6 表示 IPv4 地址的可能方式有多种)。在大多数情况下,如果 IPv6 地址代表 IPv4 地址,则 ipv4 地址位于低 4 个字节,因此您可以按如下方式获得派生的 IPv4 地址。之后,您可以转换回字节,对于 IPv4,这将仅为 4 个字节。
if(addr.isIPv4Compatible() || addr.isIPv4Mapped()) {
IPv4Address derivedIpv4Address = addr.getEmbeddedIPv4Address();
byte ipv4Bytes[] = derivedIpv4Address.getBytes();
...
}
The javadoc is available at the link.
链接中提供了 javadoc。