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"

Recent search terms:

  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1884 sg7SHFizLk6bYvWzWsGBdMjDqvAVUKA-BTXdujNP9AnFFKJpkd6TPWQ45NhO1O4B c5408587a4a11b2f57c93b97269b77be31a7b644&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1839 OCwFtMRm-RxledBSVgxCLYvCnxgOg7uG9ShqdlBPV4JTwk7d9NZC2FLL4qa1F4ud 77695f16c8d49b581349d20a46ff43582333a8da&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1837 PBIubi8cFblg25e-z1x6oAI-Tq_fHmXBsBBd2bmw8KiErBlpQg3vqnblJ7yblIvb 47f9e802e97dbe1fe6ed159899d08abf9c1b3692&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1835 iyDobdDy1vXo3IqPIHP20_VqP-AqGOV6p8ZaJO6Hjq5DmQ_ZmGn5gZsjpY98YcA9 3e103b0a1b904871dc72d939794d881240e75f5e&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1835 ApLOgBwt6efsy3O-mFM_M9WK93lTUmf-8y2rBABiAV-8KjVnqVlYMEHGHx-uWG1X 934646a2f12cf79a5a795606c65e96ba0e14e7f5&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1834 wVoY8vfDwE_eiuf0nZCSJel6fNTtWoK-FIb1-kcmkmSzAqaN7_7GeUly_Teb0mSR 1cda541707d873564470f8bbf31dc7cb33f5dff2&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1834 URiL3cNCl0h-POVOJ1uYwRws0VTx3e3jGCvpucE8-OmmsnJxl8Sgkm9Oi7MKtRam 4720e1627a1ecdacc24a18d44222f0e6b9bbabba&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1834 3sMSyKwIzMxcH7oT9WX7K8HXJTXNDQcNlk8WOjAeP7fHCVdAlvgdmLXwa-m2-p1j 6c7143588df4183a625b72733cad569433359049&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1833 _uchCATpFW0FG3HUpEajnuWOZOMfy7IiWuMSKTkZUieKC5Pse_F1ZgA4dd-t5KmD f3a6d0b3388d4f50a31f8f6c1572ce560b90fd5c&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1832 PyPqCTCx9tjc6aIZz_hV0ZSF1XSjcqI8QYGvb7gb01j6imhj_Tfg5vKckyJNIYS6 1b786b2dac5a44b05f2ea786fee9e6bbb4264f2c&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme

Leave a Reply