I want to write an online converter to convert different data types. Most of my requirements are simply solved by PHP functions but I'm facing trouble in achieving the result for converting binary data to ASCII characters. Is there any possible with PHP (preferably) or JavaScript? Here is an online converter which converts binary data to ASCII but I don't know how it works. (Hint: Many of its other converters are using PHP)
3 Answers
Try this:
$input = '01101100011011110111011001100101';
$output = '';
for($i=0; $i<strlen($input); $i+=8) {
$output .= chr(intval(substr($input, $i, 8), 2));
}
echo $output;
1 Comment
Rehmat
Thank you for your answer, it almost solved my issue :) One last question before I mark this answer as final one, can you please let me know that the converter I mentioned in my questions shows the ASCII value for "
1100101101" as a question mark while your answer outputs it as "Ë". Can you please clear my confusion here?I have written binary to ASCII converter https://www.bin-dec-hex.com/binary-to-text-ascii-converter/ to learn something new.
For converting binary to ASCII I use this function:
function binToAscii($bin) {
$text = array();
$bin = str_split($bin, 8);
for($i=0; count($bin)>$i; $i++)
$text[] = chr(bindec($bin[$i]));
return implode($text);
}