0

I'm looking to convert a file to a binary string, preferably using PHP. Is this possible?

MORE INFO: I'd like to have users upload a small file through a form, and return the binary representation of that file as a binary string - i.e ones and zeros.

1
  • Literal '0's and '1's? Because all files are binary. Commented Mar 17, 2011 at 4:27

1 Answer 1

4
$str = 'hello world';
$length = strlen($str);
$result = '';

for ($i = 0; $i < $length; $i++) {
    $result .= str_pad(decbin(ord($str[$i])), 8, '0', STR_PAD_LEFT);
}

echo $result;
Sign up to request clarification or add additional context in comments.

3 Comments

@mario: yep, missed that. Thanks
Thank you. Pointers are welcome, but a working example is great. I'll check it out and integrate it into my form. Thanks again.
can we change into string again?

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.