7

I have base 64 encoded string that looks something like this.

cuVrcYvlqYze3OZ8Y5tSqQY205mcquu0GsHkgXe4bPg=

I have tried base64_decode and output is.

råkq‹å©ŒÞÜæ|c›R©6Ó™œªë´Áäw¸lø

I think I may be doing something wrong. I appreciate any help to convert base64 string to binary array.

Thanks

5
  • 2
    What kind of data do you expect? Commented Sep 19, 2010 at 10:33
  • You are interpreting the binary data as text, which is obviously not what you want. What exactly do you mean by binary array? Commented Sep 19, 2010 at 10:35
  • It doesn't look like that's a valid base64 encoded string - did you encode that string yourself, or did you get it from somewhere else? Commented Sep 19, 2010 at 11:02
  • xil3, if you're expecting text as the output, then no, the base64 encoding is proably incorrect. However, base64 is often used to encode binary data too (which shouldn't be interpreted as plain text) Commented Sep 19, 2010 at 11:03
  • I got it from application provider. And I expect the output something like this 01110010 11100101 01101011 01110001 10001011 11100101 10101001 10001100 11011110 11011100 11100110 01111100 01100011 10011011 01010010 10101001 00000110 00110110 11010011 10011001 10011100 10101010 11101011 10110100 00011010 11000001 11100100 10000001 01110111 10111000 01101100 11111000 Commented Sep 19, 2010 at 11:14

2 Answers 2

8

like this

$a = base64_decode("cuVrcYvlqYze3OZ8Y5tSqQY205mcquu0GsHkgXe4bPg=");
$b = array();
foreach(str_split($a) as $c)
    $b[] = sprintf("%08b", ord($c));
print_r($b);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I am trying to base64_decode($key) and create a authentication signature like this hash_hmac("sha1", $ID, base64_decode($key), true); but it is not working. Am I doing something wrong? I appreciate your help.
0

You already are getting binary data back from base64_decode (if the encoded data was in fact binary), only this binary data is interpreted as encoding for some text by whatever you're outputting to (browser?). A "0011010110011001" output itself would also only be text, which would be encoded using some (different) binary stream. The computer does not work with 1's and 0's internally, contrary to popular believe. If you want to visualize binary data in the form of 1's and 0's, you'll need to do the binary/text conversion yourself. Usually that's a pretty pointless thing to do, though.

You're probably already doing the right thing. Your mistake is in expecting binary data to be represented as "0100101010".

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.