php - How to convert an array (with TRUE and FALSE values) to binary string to ASCII characters?

Solution:
This should work for you:
First I go through each element with and replace
TRUE
->1
,FALSE
->0
. Then I it into a string.
After this I simply your string into an array of 8 bits (1 byte). Then I loop through each array element with
{-code-1}
, convert the binary to dec with , and then get the ASCII character of it with
. (Also note, that I used
to make sure each element has 8 bits, otherwise I would fill it up with 0's).
Code:
<?php
//Equivalent to: 011001000110111101100111
$permissions = [FALSE, TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE];
$binaryPermissionsString = implode("", array_map(function($v){
return $v == TRUE ? 1 : 0;
}, $permissions));
$binaryCharacterArray = str_split($binaryPermissionsString, 8);
$text = implode("", array_map(function($v){
return chr(bindec(sprintf("%08d", $v)));
}, $binaryCharacterArray));
echo $text;
?>
output:
dog
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: sqlstate[23000]: integrity constraint violation: 1452 cannot add or update a child row: a foreign key constraint fails
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.