note 93085 added to function.pack
| From: | Anonymous at osu1 dot php dot net | Date: | Sat, 22 Aug 2009 03:30:49 +0000 |
| Subject: | note 93085 added to function.pack | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-159783@lists.php.net to get a copy of this message | ||
Coder's example is basically an explanation of bindec() and decbin(), not pack() and unpack().
Here's some code to convert a string binary expression into its binary-string equivalent and
vice versa.
(Would be even simpler if pack/unpack offered a 'b' format code....)
<?php
function bin2bstr($input)
// Convert a binary expression (e.g., "100111") into a binary-string
{
if (!is_string($input)) return null; // Sanity check
// Pack into a string
return pack('H*', base_convert($input, 2, 16));
}
function bstr2bin($input)
// Binary representation of a binary-string
{
if (!is_string($input)) return null; // Sanity check
// Unpack as a hexadecimal string
$value = unpack('H*', $input);
// Output binary representation
return base_convert($value[1], 16, 2);
}
// Returns string(3) "ABC"
var_dump(bin2bstr('01000001 01000010 01000011'));
// Returns string(24) "010000010100001001000011"
var_dump(bstr2bin('ABC'));
?>
----
Server IP: 208.69.120.35
Probable Submitter: 67.237.228.46
----
Manual Page -- http://www.php.net/manual/en/function.pack.php
Edit -- https://master.php.net/note/edit/93085
Del: integrated -- https://master.php.net/note/delete/93085/integrated
Del: useless -- https://master.php.net/note/delete/93085/useless
Del: bad code -- https://master.php.net/note/delete/93085/bad+code
Del: spam -- https://master.php.net/note/delete/93085/spam
Del: non-english -- https://master.php.net/note/delete/93085/non-english
Del: in docs -- https://master.php.net/note/delete/93085/in+docs
Del: other reasons-- https://master.php.net/note/delete/93085
Reject -- https://master.php.net/note/reject/93085
Search -- https://master.php.net/manage/user-notes.php