Nasıl bir dize php bir ikili dizi dönüştürebilirim?
let say that you want to convert $stringA="Hello" to binary. First take the first character with ord() function; this wil give you the ascii value of the character which is decimal. In this case is 72. now convert it to binary with the dec2bin() function. then take the next function. you can find how these functions work at http://www.php.net.
VEYA bu kod parçası kullanın
<?php
//call the function like this : asc2bin("text to convert");
function asc2bin($string)
{
$result = '';
$len = strlen($string);
for ($i = 0; $i < $len; $i++)
{
$result .=sprintf("%08b",ord($string{$i}));
}
return $result;
}
//if you want to test it remove the comments
//$test=asc2bin("Hello world");
//echo "Hello world ascii2bin conversion =".$test."<br/>";
//call the function like this : bin2ascii($variableWhoHoldsTheBinary)
function bin2ascii($bin)
{
$result = '';
$len = strlen($bin);
for ($i = 0; $i < $len; $i += 8)
{
$result .= chr(bindec(substr($bin,$i,8)));
}
return $result;
}
//if you want to test it remove the comments
//$backAgain=bin2ascii($test);
//echo "Back again with bin2ascii() =".$backAgain;
?>
Dürüst olmak gerekirse, ben ikili dizi (CBS ya da açıklık yoktu) ile ne demek emin değilim. Eğer bir karakter başına elemanı, daha sonra str_split built-in istediğiniz bir c-tarzı dizi kastediyorsanız.