def to_ipv4(self):
        """
        Convert (an IPv6) IP address to an IPv4 address, if possible.

        Only works for IPv4-compat (::/96), IPv4-mapped (::ffff/96), and 6-to-4
        (2002::/16) addresses.

        >>> ip = IP('2002:c000:022a::')
        >>> print(ip.to_ipv4())
        192.0.2.42
        """
        if self.v == 4:
            return self
        else:
            if self.bin().startswith('0' * 96):
                return IP(int(self), version=4)
            elif self.bin().startswith('0' * 80 + '1' * 16):
                return IP(int(self) & MAX_IPV4, version=4)
            elif int(self) & BASE_6TO4:
                return IP((int(self) - BASE_6TO4) >> 80, version=4)
            else:
                return ValueError('%s: IPv6 address is not IPv4 compatible or mapped, '
                                  'nor an 6-to-4 IP' % self.dq)