PHP以真正的二进制方式读取二进制文件

发布于 2024-12-08 07:00:46 字数 404 浏览 0 评论 0原文

我在谷歌上搜索了我的问题,但没有找到解决方案。 我想读取一个文件并将缓冲区转换为二进制,如 10001011001011001。

如果我从文件中有类似的内容

bmoov���lmvhd�����(tF�(tF�_�
K�T��������������������������������������������@���������������������������������trak���\tkh
d����(tF�(tF������� K������������������������������������������������@������������$edts��

如何将所有字符(也包括这个东西��)转换为 101010101000110010 表示?

我希望有人能帮助我:)

I searched google for my problem but found no solution.
I want to read a file and convert the buffer to binary like 10001011001011001.

If I have something like this from the file

bmoov���lmvhd�����(tF�(tF�_�
K�T��������������������������������������������@���������������������������������trak���\tkh
d����(tF�(tF������� K������������������������������������������������@������������$edts��

How can I convert all characters (including also this stuff ��) to 101010101000110010 representation??

I hope someone can help me :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

所有深爱都是秘密 2024-12-15 07:00:46

对每个字节使用 ord() 获取其十进制值,然后 sprintf 以二进制形式打印它(并通过在前面填充 0 强制每个字节包含 8 位)。

<?php
$buffer = file_get_contents(__FILE__);
$length = filesize(__FILE__);

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

$_buffer = '';
for ($i = 0; $i < $length; $i++) {
  $_buffer .= sprintf("%08b", ord($buffer[$i]));
}

var_dump($_buffer);

$ php 测试.php

string(2096) "00111100001111110111000001101000011100000000101000100100011000100111010101100110011001100110010101110010001000000011110100100000011001100110100101101100011001010101111101100111011001010111010001011111011000110110111101101110011101000110010101101110011101000111001100101000010111110101111101000110010010010100110001000101010111110101111100101001001110110000101000100100011011000110010101101110011001110111010001101000001000000011110100100000011001100110100101101100011001010111001101101001011110100110010100101000010111110101111101000110010010010100110001000101010111110101111100101001001110110000101000001010011010010110011000100000001010000010000100100100011000100111010101100110011001100110010101110010001000000111110001111100001000000010000100100100011011000110010101101110011001110111010001101000001010010010000001111011000010100010000000100000011001000110100101100101001010000010001001010010011001010110000101100100011010010110111001100111001000000110010101110010011100100110111101110010010111000110111000100010001010010011101100001010011111010000101000001010001001000101111101100010011101010110011001100110011001010111001000100000001111010010000000100111001001110011101100001010011001100110111101110010001000000010100000100100011010010010000000111101001000000011000000111011001000000010010001101001001000000011110000100000001001000110110001100101011011100110011101110100011010000011101100100000001001000110100100101011001010110010100100100000011110110000101000100000001000000010010001011111011000100111010101100110011001100110010101110010001000000010111000111101001000000111001101110000011100100110100101101110011101000110011000101000001000100010010100110000001110000110010000100010001011000010000001100100011001010110001101100010011010010110111000101000011011110111001001100100001010000010010001100010011101010110011001100110011001010111001001011011001001000110100101011101001010010010100100101001001110110000101001111101000010100000101001110110011000010111001001011111011001000111010101101101011100000010100000100100010111110110001001110101011001100110011001100101011100100010100100111011"

Use ord() on each byte to get its decimal value and then sprintf to print it in binary form (and force each byte to include 8 bits by padding with 0 on front).

<?php
$buffer = file_get_contents(__FILE__);
$length = filesize(__FILE__);

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

$_buffer = '';
for ($i = 0; $i < $length; $i++) {
  $_buffer .= sprintf("%08b", ord($buffer[$i]));
}

var_dump($_buffer);

$ php test.php

string(2096) "00111100001111110111000001101000011100000000101000100100011000100111010101100110011001100110010101110010001000000011110100100000011001100110100101101100011001010101111101100111011001010111010001011111011000110110111101101110011101000110010101101110011101000111001100101000010111110101111101000110010010010100110001000101010111110101111100101001001110110000101000100100011011000110010101101110011001110111010001101000001000000011110100100000011001100110100101101100011001010111001101101001011110100110010100101000010111110101111101000110010010010100110001000101010111110101111100101001001110110000101000001010011010010110011000100000001010000010000100100100011000100111010101100110011001100110010101110010001000000111110001111100001000000010000100100100011011000110010101101110011001110111010001101000001010010010000001111011000010100010000000100000011001000110100101100101001010000010001001010010011001010110000101100100011010010110111001100111001000000110010101110010011100100110111101110010010111000110111000100010001010010011101100001010011111010000101000001010001001000101111101100010011101010110011001100110011001010111001000100000001111010010000000100111001001110011101100001010011001100110111101110010001000000010100000100100011010010010000000111101001000000011000000111011001000000010010001101001001000000011110000100000001001000110110001100101011011100110011101110100011010000011101100100000001001000110100100101011001010110010100100100000011110110000101000100000001000000010010001011111011000100111010101100110011001100110010101110010001000000010111000111101001000000111001101110000011100100110100101101110011101000110011000101000001000100010010100110000001110000110010000100010001011000010000001100100011001010110001101100010011010010110111000101000011011110111001001100100001010000010010001100010011101010110011001100110011001010111001001011011001001000110100101011101001010010010100100101001001110110000101001111101000010100000101001110110011000010111001001011111011001000111010101101101011100000010100000100100010111110110001001110101011001100110011001100101011100100010100100111011"
酒解孤独 2024-12-15 07:00:46

您可以做的是将文件读入字符串变量,然后使用 以二进制数字表示形式打印字符串sprintfDocs

$string = file_get_contents($file);

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

如果您只是寻找十六进制表示形式,则可以使用 bin2hex文档

echo bin2hex($string);

如果您正在寻找更好形式的十六进制转储,请参阅相关问题:

On thing you could do is to read the file into a string variable, then print the string in your binary number representation with the use of sprintfDocs:

$string = file_get_contents($file);

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

If you're just looking for a hexadecimal representation, you can use bin2hexDocs:

echo bin2hex($string);

If you're looking for a nicer form of hexdump, please see the related question:

未蓝澄海的烟 2024-12-15 07:00:46

按字读取文件(一次 32 位)比按字节读取文件更快:

$s = file_get_contents("filename");
foreach(unpack("L*", $s) as $n) 
    $buf[] = sprintf("%032b", $n);

Reading a file word-wise (32 bits at once) would be faster than byte-wise:

$s = file_get_contents("filename");
foreach(unpack("L*", $s) as $n) 
    $buf[] = sprintf("%032b", $n);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文