How to convert base64 string to binary array using php

六月ゝ 毕业季﹏ 提交于 2019-12-23 12:54:46

问题


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


回答1:


like this

$a = base64_decode("cuVrcYvlqYze3OZ8Y5tSqQY205mcquu0GsHkgXe4bPg=");
$b = array();
foreach(str_split($a) as $c)
    $b[] = sprintf("%08b", ord($c));
print_r($b);



回答2:


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".



来源:https://stackoverflow.com/questions/3745338/how-to-convert-base64-string-to-binary-array-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!