PHP: how to convert ip address to binary string or hex string


With pure PHP, you can convert an IPv4 address to long with the function ip2long, but how to convert an Ipv6 address to long. You can’t do this in the 32bits system. You only convert an ip address to binary string or hex string with some method we provide below:

<?php

/**
 * @author www.tutorialspots.com
 * @copyright 2016
 **/
 
function ip2bin($ip) 
{ 
    if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false) 
        //return sprintf("%032s",base_convert(ip2long($ip),10,2)); 
        return base_convert(ip2long($ip),10,2); 
    if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) 
        return false; 
    if(($ip_n = inet_pton($ip)) === false) return false; 
    $bits = 15;  
    $ipbin = '';
    while ($bits >= 0) 
    { 
        $bin = sprintf("%08b",(ord($ip_n[$bits]))); 
        $ipbin = $bin.$ipbin; 
        $bits--; 
    } 
    return $ipbin; 
} 

function bin2ip($bin) 
{ 
   if(strlen($bin) <= 32)  
       return long2ip(base_convert($bin,2,10)); 
   if(strlen($bin) != 128) 
       return false; 
   $pad = 128 - strlen($bin); 
   for ($i = 1; $i <= $pad; $i++) 
   { 
       $bin = "0".$bin; 
   } 
   $bits = 0; 
   $ipv6 = '';
   while ($bits <= 7) 
   { 
       $bin_part = substr($bin,($bits*16),16); 
       $ipv6 .= dechex(bindec($bin_part)).":"; 
       $bits++; 
   } 
   return inet_ntop(inet_pton(substr($ipv6,0,-1))); 
}

function ip2hex($ip) 
{ 
    if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false) 
        return sprintf("%08x",ip2long($ip)); 
    if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) 
        return false; 
    if(($ip_n = inet_pton($ip)) === false) return false; 
    $bits = 15;  
    $ipbin = '';
    while ($bits >= 0) 
    { 
        $bin = sprintf("%02x",(ord($ip_n[$bits]))); 
        $ipbin = $bin.$ipbin; 
        $bits--; 
    } 
    return $ipbin; 
} 

function hex2ip($bin) 
{ 
   if(strlen($bin) <= 8)  
       return long2ip(base_convert($bin,16,10)); 
   if(strlen($bin) != 32) 
       return false; 
   $pad = 32 - strlen($bin); 
   for ($i = 1; $i <= $pad; $i++) 
   { 
       $bin = "0".$bin; 
   } 
   $bits = 0; 
   $ipv6 = '';
   while ($bits <= 7) 
   { 
       $bin_part = substr($bin,($bits*4),4); 
       $ipv6 .= $bin_part.":"; 
       $bits++; 
   } 
   return inet_ntop(inet_pton(substr($ipv6,0,-1))); 
}

Some example:
Example 1:

$ips = array(
    '::192.168.1.1',
    '0:0:0:0:0:0:192.168.1.1',
    '192.168.1.1',
    '::C0A8:0101',
    '0:0:0:0:0:0:C0A8:0101'
);
$finals = array();
foreach($ips as $ip) {
    $finals[] = ip2hex($ip);
}

var_dump($finals);

Result:

array(5) {
  [0]=>
  string(32) "000000000000000000000000c0a80101"
  [1]=>
  string(32) "000000000000000000000000c0a80101"
  [2]=>
  string(8) "c0a80101"
  [3]=>
  string(32) "000000000000000000000000c0a80101"
  [4]=>
  string(32) "000000000000000000000000c0a80101"
}

Example 2:

$ips = array(
    '::192.168.1.1',
    '0:0:0:0:0:0:192.168.1.1',
    '192.168.1.1',
    '::C0A8:0101',
    '0:0:0:0:0:0:C0A8:0101'
);
$finals = array();
foreach($ips as $ip) {
    $finals[] = ip2bin($ip);
}

var_dump($finals);

Result:

array(5) {
  [0]=>
  string(128) "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011000000101010000000000100000001"
  [1]=>
  string(128) "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011000000101010000000000100000001"
  [2]=>
  string(30) "111111010101111111111011111111"
  [3]=>
  string(128) "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011000000101010000000000100000001"
  [4]=>
  string(128) "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011000000101010000000000100000001"
}

Example 3:

var_dump(hex2ip('000000000000000000000000c0a80002'));

Result:

string(13) "::192.168.0.2"

Leave a Reply