By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,921 Members | 1,466 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,921 IT Pros & Developers. It's quick & easy.

Handling Binary Strings

P: n/a
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().

Can someone suggest some other way to preserve binary data into ASCII
and back in php?
Nov 5 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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";
}
}
?>

Nov 5 '06 #2

P: n/a
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
Nov 5 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.