How to convert an address from IPv4 to IPv6

Hybrid dual-stack IPv6/IPv4 implementations typically support a special class of addresses, the IPv4-mapped addresses. For more check the following link:

http://en.wikipedia.org/wiki/IPv6#IPv4-mapped_IPv6_addresses

For converting IPv4 to mapped IPv6, you can use the following:

String ip = "127.0.0.1"; 
String[] octets = ip.split("\\.");
byte[] octetBytes = new byte[4];
 for (int i = 0; i < 4; ++i) {
            octetBytes[i] = (byte) Integer.parseInt(octets[i]);
}

byte ipv4asIpV6addr[] = new byte[16];
ipv4asIpV6addr[10] = (byte)0xff;
ipv4asIpV6addr[11] = (byte)0xff;
ipv4asIpV6addr[12] = octetBytes[0];
ipv4asIpV6addr[13] = octetBytes[1];
ipv4asIpV6addr[14] = octetBytes[2];
ipv4asIpV6addr[15] = octetBytes[3];

Also check this


There is no IPv4 to IPv6 mapping that is meaningful. things like 0.0.0.0 and 127.0.0.1 are special cases in the spec, so they have equivalent meaning. But given an IPv4 address it tells you nothing about what its specific IPv6 address would be. You can use a DNS lookup to see if a given IP address resolves to a host which in turn resolves to an IPv6 address in addition to an IPv4 address, but the DNS server would have to be configured to support that for the specific machine.

Tags:

Java

Ipv6