6

How can I read the binary code(to get the 1s and 0s) of a file.

$filename = "something.mp3";
$handle = fopen($filename, "rb");
$contents = fread($handle, filesize($filename));
fclose($handle);

I tried this but it shows some strange characters... I presume that this is the formated binary? I was hoping to get the 1's and 0's instead.

Also I am not looking only .mp3 files it could be anything .e.g: .txt , .doc , .mp4, .php, .jpg, .png etc....

3
  • 1
    How did you examine $contents - if it was echo or print I'd assume they interpret things as characters. Commented Feb 4, 2010 at 14:55
  • echo is what i used... does it make a difference?(not a rude question) Commented Feb 4, 2010 at 14:55
  • now i have just tested it with print var_dump same result... Commented Feb 4, 2010 at 14:57

3 Answers 3

13

Files are stored on the computer in binary form indeed, but the 1s and 0s are stored together in groups of 8 (called bytes). Now, traditionally, each byte may be represented by an ASCII character because of the fact that there are 256 possible values that can be represented in a byte - which happens to coincide with the total number of different ASCII characters available (this was not a coincidence but actually by design).

That being said, what you are getting back from the fread function is what you're supposed to get: i.e. the contents of the file.

If you want to see the 1s an 0s you will need to print each byte that your receive into it's base 2 representation. You can achieve that using a function such as base_convert or by writing your own.

$filename = "something.mp3";
$handle = fopen($filename, "rb");
$fsize = filesize($filename);
$contents = fread($handle, $fsize);
fclose($handle);

// iterate through each byte in the contents
for($i = 0; $i < $fsize; $i++)
{ 
   // get the current ASCII character representation of the current byte
   $asciiCharacter = $contents[$i];
   // get the base 10 value of the current characer
   $base10value = ord($asciiCharacter);
   // now convert that byte from base 10 to base 2 (i.e 01001010...)
   $base2representation = base_convert($base10value, 10, 2);
   // print the 0s and 1s
   echo($base2representation);
}

NOTE

If you have a string of 1s and 0s (the base 2 representation of a character) you can convert it back to the character like so:

$base2string = '01011010';
$base10value = base_convert($base2string, 2, 10);  // => 132
$ASCIICharacter = chr($base10value);               // => 'Z'
echo($ASCIICharacter);                             // will print Z
Sign up to request clarification or add additional context in comments.

6 Comments

ok i got an example from hexadecimal to binary conversion... how would i convert it from that file to binary using the base_convert
of course it is. I'm not sure what you're tying to do but keep in mind that you do have the ASCII characters at first
if i have a file something.php that has "hello" doing the above = "0110100001100101011011000110110001101111" so how would if i put "0110100001100101011011000110110001101111" how would i get "hello"?.......... I am trying to compress the binary (my own way)...
please see the note in my response. it shows how to do the conversion but please do some more reading about bit, bytes and the way computers represent data before you attempt to write your own data compression algorithm.. in PHP..
Hi Miky Dinescu, Is there a way to store in .doc file from the binary code to its original from. I am looking for to store images in .doc file using php.
|
6

Here you go, the 1s and the 0s:

$filename = "something.mp3";
$handle = fopen($filename, "rb");
$contents = fread($handle, filesize($filename));
for ($i = 0; $i < strlen($contents); $i++) {
    $binary = sprintf("%08d", base_convert(ord($contents[$i]), 10, 2));
    echo $binary . " ";
}
fclose($handle);

2 Comments

thank you man this is amazing.... could you help me to reverse this back to that strange charcter thing?
Sure, just replace the complete for loop with echo $contents;.
1

Why not use the PHP function decbin?

for($i = 0; $i < $fsize; $i++){

      $base10value = ord($contents[$i]);
      echo decbin($base10value);
}

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.