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)
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)
Share Improve this question asked Feb 21, 2016 at 12:30 RehmatRehmat 5,1115 gold badges25 silver badges39 bronze badges3 Answers
Reset to default 4Try this:
$input = '01101100011011110111011001100101';
$output = '';
for($i=0; $i<strlen($input); $i+=8) {
$output .= chr(intval(substr($input, $i, 8), 2));
}
echo $output;
I have written binary to ASCII converter https://www.bin-dec-hex./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);
}
this is my code, and works
file_put_contents('sample.pdf', pack("H*", str_replace("0x", "", $hex)));
replace "sample.pdf" with any name you like and $hex variable with your data from database