Reply to Re: Handling Binary Strings

Your name:

Reply:


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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация