4

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

4
  • 1
    A binary file isn't really binary. It's... uhhh... machine code...? Commented Oct 4, 2011 at 17:48
  • 3
    You want to convert each byte to an 8-character string of 1's and 0's? Commented Oct 4, 2011 at 17:51
  • 1
    You can look into pack. You should be able to do something like pack("h", $myString) to get a hexadecimal representation. Commented Oct 4, 2011 at 17:52
  • @Andrew: more likely unpack, but h is not the right format specifier according to the question and I think pack/unpack does not have one for this in PHP. Commented Oct 4, 2011 at 18:19

3 Answers 3

11

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"
Sign up to request clarification or add additional context in comments.

1 Comment

FYI: strlen() not working on strings containing nullbytes is just wrong: echo strlen("\0bc"); # 3.
4

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:

2 Comments

strlen() on a binary string won't work, for all we know there might be \0 in there. Also he didn't explicitly state it but padding each byte to 8 bits would probably be needed in order for the result to be of any use.
@Lepidosteus: Won't work? I assume you're mixing C strings with PHP strings here, so feel free to extend your knowledge about strings in the PHP manual: php.net/string and php.net/strlen
1

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

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.