最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Converting Binary Data to ASCII with PHP, Any Possibility? - Stack Overflow

programmeradmin0浏览0评论

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 badges
Add a ment  | 

3 Answers 3

Reset to default 4

Try 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

发布评论

评论列表(0)

  1. 暂无评论