note 62130 deleted from function.decbin by betz

From: Date: Mon, 20 Feb 2006 23:40:34 +0000
Subject: note 62130 deleted from function.decbin by betz
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-104093@lists.php.net to get a copy of this message
Note Submitter: ---- ... a fast function to convert Xavier's bit sequence to a binary string.

<?php
function bs2s($bitseq) {
 if (strlen($bitseq) % 8 != 0) return false;
 $str = "";
 for($i = 0; $i < strlen($bitseq); $i += 8) {
 $str .= chr(bindec(substr($bitseq,$i,8)));
 }
 return $str;
}
?>

Note that I used
<?php
if (strlen($bitseq) % 8 != 0) return false;
?>
to check if the bit sequence really is a bitsequence.
Of course you can also check that the bin sequence only contains 0s and 1s, but I don't think that this is usefull, because you only convert bin sequences with 0s and 1s.
% 8 is just for beeing sure that you didn't misspelled, the rest is irrelevant.
You can call the function BitSequence2BinString, but I think that bs2s is easier to call because it's shorter.

« previous php.notes (#104093) next »