note 13921 added to function.pack.php
| From: | mfisch at php2 dot chek dot com | Date: | Tue, 10 Jul 2001 13:53:36 +0000 |
| Subject: | note 13921 added to function.pack.php | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-15161@lists.php.net to get a copy of this message | ||
If you are trying to do ascii <--> binary conversions like me;
you probably found that unlike the perl pack functions, these wont help too much. Attached are two
functions I wrote to accomplish this task.
<br>
function bin2asc ($binary)
{
$i = 0;
while ( strlen($binary) > 3 )
{
$byte[$i] = substr($binary, 0, 8);
$byte[$i] = base_convert($byte[$i], 2, 10);
$byte[$i] = chr($byte[$i]);
$binary = substr($binary, 8);
$ascii = "$ascii$byte[$i]";
}
return $ascii;
}
<br>
function asc2bin ($ascii)
{
while ( strlen($ascii) > 0 )
{
$byte = ""; $i = 0;
$byte = substr($ascii, 0, 1);
while ( $byte != chr($i) ) { $i++; }
$byte = base_convert($i, 10, 2);
$byte = str_repeat("0", (8 - strlen($byte)) ) . $byte; # This is an endian
(architexture) specific line, you may need to alter it.
$ascii = substr($ascii, 1);
$binary = "$binary$byte";
}
return $binary;
}
<br>
Im not sure these are the most efficient functions, but surely alot faster than loading up a perl
interpreter for every binary conversion =)
http://www.php.net/manual/en/function.pack.php