Допустим, вы хотите преобразовать $ stringA = "Hello" в двоичный файл.
Сначала возьмите первый символ с функцией ord (). Это даст вам значение ASCII символа, который является десятичным. В данном случае это 72.
Теперь преобразуйте его в двоичный файл с помощью функции dec2bin ().
Затем возьмите следующую функцию.
Вы можете найти, как эти функции работают в http://www.php.net.
ИЛИ используйте этот кусок кода:
<?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;
?>