PHP将大型二进制代码转换为字符串

时间:2018-02-16 18:41:56

标签: php binary base-conversion

我需要将大量二进制文件转换为字符串。

我用来获取二进制代码的代码是:

$buffer = file_get_contents('test.php');
$length = filesize('test.php');
if (!$buffer || !$length) {
  die("Reading error\n");
}
$_buffer = '';
for ($i = 0; $i < $length; $i++) {
  $_buffer .= sprintf("%08b", ord($buffer[$i]));
}

我尝试使用base_convert()。这给出了以下错误消息:

<?php 
pack('H*', base_convert($_buffer , 2, 16));
?>
  

警告:base_convert():数字太大了   第25行的C:\ xampp \ htdocs \ decrypt.php

有什么建议吗?

二进制代码是

  

0011110000111111011100000110100001110000000011010000101000100100011000100111010101100110011001100110010101110010001000000011110100100000011001100110100101101100011001010101111101100111011001010111010001011111011000110110111101101110011101000110010101101110011101000111001100101000001001110111010001100101011100110111010000101110011100000110100001110000001001110010100100111011000011010000101000100100011011000110010101101110011001110111010001101000001000000011110100100000011001100110100101101100011001010111001101101001011110100110010100101000001001110111010001100101011100110111010000101110011100000110100001110000001001110010100100111011000011010000101000001101000010100110100101100110001000000010100000100001001001000110001001110101011001100110011001100101011100100010000001111100011111000010000000100001001001000110110001100101011011100110011101110100011010000010100100100000011110110000110100001010001000000010000001100100011010010110010100101000001000100101001001100101011000010110010001101 0010110111001100111001000000110010101110010011100100110111101110010010111000110111000100010001010010011101100001101000010100111110100001101000010100000110100001010001001000101111101100010011101010110011001100110011001010111001000100000001111010010000000100111001001110011101100001101000010100110011001101111011100100010000000101000001001000110100100100000001111010010000000110000001110110010000000100100011010010010000000111100001000000010010001101100011001010110111001100111011101000110100000111011001000000010010001101001001010110010101100101001001000000111101100001101000010100010000000100000001001000101111101100010011101010110011001100110011001010111001000100000001011100011110100100000011100110111000001110010011010010110111001110100011001100010100000100010001001010011000000111000011000100010001000101100001000000110111101110010011001000010100000100100011000100111010101100110011001100110010101110010010110110010010001101001010111010010100100101001001110110000110100001010011111010000110100001 0100110010101100011011010000110111100100000011100000110000101100011011010110010100000100111010010000010101000100111001011000110001001100001011100110110010101011111011000110110111101101110011101100110010101110010011101000010100000100100010111110110001001110101011001100110011001100101011100100010110000110010001011000011000100110110001010010010100100111011000011010000101001100101011000110110100001101111001000000010010001011111011000100111010101100110011001100110010101110010001011100010001001011100011011100010001000111011000011010000101000100100011001100110100101101100011001010010000000111101001000000010011101110100011001010111001101110100001011100111000001101000011100000010011100111011000011010000101000100100011100110111010001110010011010010110111001100111001000000011110100100000011001100110100101101100011001010101111101100111011001010111010001011111011000110110111101101110011101000110010101101110011101000111001100101000001001000110011001101001011011000110010100101001001110110000110100001 0100000110100001010011001100110111101110010001010000010010001101100001111010111001101110100011100100110110001100101011011100010100000100100011100110111010001110010011010010110111001100111001010010010110000100000001001000110100100111101001100000011101100100000001001000110100100111100001001000110110000111011001000000010010001101001001010110010101100101001000011010000101001111011000011010000101000100000001000000010000000100000011100000111001001101001011011100111010001100110001010000010011100100101001100000011100001100010001001110010110000100000011011110111001001100100001010000010010001110011011101000111001001101001011011100110011101011011001001000110100101011101001010010010100100111011000011010000101001111101

输出应为:

<?php
$buffer = file_get_contents('test.php');
$length = filesize('test.php');

if (!$buffer || !$length) {
  die("Reading error\n");
}

$_buffer = '';
for ($i = 0; $i < $length; $i++) {
  $_buffer .= sprintf("%08b", ord($buffer[$i]));
}
echo pack('H*',base_convert($_buffer,2,16));
echo $_buffer."\n";
$file = 'test.php';
$string = file_get_contents($file);

for($l=strlen($string), $i=0; $i<$l; $i++)
{
    printf('%08b', ord($string[$i]));
}

0 个答案:

没有答案