|
Posted by Pedro Graca on 11/05/06 19:59
CharlesL wrote:
> I am trying to handle binary strings in php. I get a binary output
> initialization vector from mcrypt as such:
>
> from mcrypt:
> $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
>
> This output may have padded nulls at the end which are significant.
>
> I would like to convert all characters including all padding nulls at
> end and convert it in a string to be saved into a database in such a way
> that I can reconstruct it back to binary again later.
>
> base64_encode and decode is not doing the job. They don't seem to
> preserve the original binary value after base64_decode().
What is a padded null?
Whit plain nulls, base64_encode() and base64_decode() work for me.
<?php
$data = "abcde\0"; // trailing null
$in = base64_encode($data);
$out = base64_decode($in);
echo strlen($data), " : ", strlen($out), "\n";
for ($i=0;$i<strlen($data);++$i) {
if ($data{$i} == $out{$i}) {
echo ord($data{$i}), " ok\n";
} else {
echo ord($data{$i}), ";", ord($out{$i}), " NOT ok\n";
}
}
?>
[Back to original message]
|