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=1841 Bbvh0h4c1KneBVBef92Py-b_-V65gDXh1ewOOUQ9wxz0y8xuWgZoeRtG7WRSYEVF 0169faa03a3d497d8e6943160e1616bdecc8c96d&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1830 XdJIslYtDERbS305gW4XdWS6HzwjUcdRoX5F8vq0sNPqvIfLbiGP-KKQG5T8jkpk 214f22d52fbda441d6504b4cba36f9de9b698c89&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1829 PsjVk4aqOKhhSF11REfCHh5BQ5FS4jA8DmT1RPRRNY3zXBoJNcmBXK-FopGjDtoL f95df357a7fef10436789c6aee26fa4d51e0559e&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1826 H_iT26Voyxv5zX5Y0iVr3vpoWRY2WnIcCQqAkynBUb1WvHXTXRrMN6EFYVI8W5K- e244765247678a2ac3539311c84d5ad3b103e98b&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1826 fNxOUSWp0ZdbAThS-G-k6_b6spcq7lfxTmsaNOHhCpYNIh1K9q2uiCR3R0LTuUK9 2cace9b94708a15028057d37d515bcff7b52be13&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1825 DNcAzl2Ka27ZKc8n2Mp1--B68f-zCMff5jTJe3b7sF_hc5XJ0k089BRF3pK4FECP 82be353d79b2f9be4b573a81fadd9965ba839707&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1823 wPkxvAR6GyzIyJdFHGHUWINuz4IpWbYnYzgs-aXhAET4VXzoNJrUjRGn5qJDZ60E 97a138ee471ba7a94eab837cf8dccb948d60afaf&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1822 pujcMpzYAmDwvzPQBPmTM3fcXTz6k1Hyp8LaBI_hjSHtDaNt8_qESaw_30RKQ_aI 8c36e7577a2fe791138938415858acfef26022fb&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1822 BtSlf1jO-4qvXHAmxFYoYgKr3359PWlKaAMGd4ykUZi2cyQUGSVp1RGe5-bJa4l9 f2f6180efb9e5f89817b356105fb59645136b6a4&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1822 1rxck-O4iQMiNNe8KKLd0yVQobY0f8AJpQiYUuBQ0Fr86TqaU2GMkpx2TgNmlTjx bc180f6a37faa85733f6094f63ddc6709c3c7649&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme

Leave a Reply