I have a list of IPv4 IPs selected from a database which stores the addresses as BINARY(16). Is there any simple way to convert the IPv6 formated address to human readable IPv4 format?
This is what the IPv4 address looks like 8ab8:7f70::
Asked
Active
Viewed 9,733 times
-5

pedmillon
- 143
- 3
- 13
-
IPv6 addresses are 128 bits, while IPv4 addresses are only 32 bits. You cannot display IPv6 addresses in the IPv4 dotted-decimal notation. IPv6 has its own notation. [RFC 5952, A Recommendation for IPv6 Address Text Representation](https://tools.ietf.org/html/rfc5952) is the IETF standard for representing IPv6 addresses. – Ron Maupin Jul 29 '16 at 17:41
-
1I have IPv4 addresses that are represented in IPv6 format e.g. 8ab8:7f70:: – pedmillon Jul 29 '16 at 17:43
-
1Then just convert the four bytes, individually, to a decimal string, and concatenate, placing a `.` between each. – Ron Maupin Jul 29 '16 at 17:45
1 Answers
-3
As Ron Maupin described, the solution is very simple
$ipv6 = "8ab8:7f70::";
$ipv4 = hexdec(substr($ipv6, 0, 2)). "." . hexdec(substr($ipv6, 2, 2)). "." . hexdec(substr($ipv6, 5, 2)). "." . hexdec(substr($ipv6, 7, 2));

pedmillon
- 143
- 3
- 13
-
2
-
3
-
this code doesn't work properly and ipv4 returned is not the same of ipv6 converted. – Anass Jun 02 '20 at 16:44