1

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 3

4

Try this:

$input = '01101100011011110111011001100101';
$output = '';
for($i=0; $i<strlen($input); $i+=8) {
  $output .= chr(intval(substr($input, $i, 8), 2));
}
echo $output;
Sign up to request clarification or add additional context in comments.

1 Comment

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?
2

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);
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.