Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP read binary file in real binary

Tags:

php

binary

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 :)

like image 687
corvus Avatar asked Dec 05 '22 20:12

corvus


2 Answers

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"
like image 194
Lepidosteus Avatar answered Dec 07 '22 09:12

Lepidosteus


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:

  • How can I get a hex dump of a string in PHP?
like image 29
hakre Avatar answered Dec 07 '22 10:12

hakre