Handling Binary Strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CharlesL

    Handling Binary Strings

    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_i v($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?
  • Pedro Graca

    #2
    Re: Handling Binary Strings

    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_i v($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";
    }
    }
    ?>

    Comment

    • Andy Hassall

      #3
      Re: Handling Binary Strings

      On Sun, 05 Nov 2006 13:45:04 -0500, CharlesL <cl@sd.nlwrot e:
      >I am trying to handle binary strings in php. I get a binary output
      >initializati on vector from mcrypt as such:
      >
      >from mcrypt:
      $iv = mcrypt_create_i v($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_encod e 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(b ase64_encode($d ata))));
      ?>

      andyh@excession /cygdrive/c/public_html
      $ php test.php
      array(1) {
      [1]=>
      string(32) "00000000000000 000000000000000 000"
      }
      AAAAAAAAAAAAAAA AAAAAAA==
      array(1) {
      [1]=>
      string(32) "00000000000000 000000000000000 000"
      }

      (And incidentally pack/unpack may give you another option)

      --
      Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
      http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

      Comment

      Working...