On Sun, 05 Nov 2006 13:45:04 -0500, CharlesL <cl@sd.nlwrote:
>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 version are you using? I'm not seeing trimming of trailing nul bytes on
PHP 5.2.0, at least:
andyh@excession /cygdrive/c/public_html
$ cat test.php
<?php
$data = str_repeat(chr(0), 16);
var_dump(unpack('H*', $data));
print base64_encode($data) . "\n";
var_dump(unpack('H*', base64_decode(base64_encode($data))));
?>
andyh@excession /cygdrive/c/public_html
$ php test.php
array(1) {
[1]=>
string(32) "00000000000000000000000000000000"
}
AAAAAAAAAAAAAAAAAAAAAA==
array(1) {
[1]=>
string(32) "00000000000000000000000000000000"
}
(And incidentally pack/unpack may give you another option)
--
Andy Hassall ::
an**@andyh.co.uk ::
http://www.andyh.co.uk http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool