note 62130 added to function.decbin

From: Date: Mon, 20 Feb 2006 23:19:44 +0000
Subject: note 62130 added to function.decbin
Groups: php.notes 
Request: Send a blank email to php-notes+get-104092@lists.php.net to get a copy of this message
... 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. ---- Server IP: 66.163.161.117 Probable Submitter: 200.107.2.45 ---- Manual Page -- http://www.php.net/manual/en/function.decbin.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+62130 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+62130&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+62130&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+62130&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+62130&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+62130&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+62130&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+62130&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+62130&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#104092) next »