How to see binary representation of variable

前端 未结 7 1305
醉梦人生
醉梦人生 2020-12-19 00:07

Is there any possibility to see binary representation of variable?

7条回答
  •  醉梦人生
    2020-12-19 00:20

    Or you may use base_convert function to convert symbol codes to binary, here's a modified function:

    function str2bin($str) 
    {
        $out=false; 
        for($a=0; $a < strlen($str); $a++)
        {
            $dec = ord(substr($str,$a,1)); //determine symbol ASCII-code
            $bin = sprintf('%08d', base_convert($dec, 10, 2)); //convert to binary representation and add leading zeros
            $out .= $bin;
        }
        return $out;
    }
    

    It is useful to convert inet_pton() result to compare ipv6 addresses in binary format (since you cannot really convert a 128-bit ipv6 address to integer, which is 32- or 64-bit in php). You can find more on ipv6 and php here (working-with-ipv6-addresses-in-php) and here (how-to-convert-ipv6-from-binary-for-storage-in-mysql).

提交回复
热议问题