Search code examples
phpflash-media-server

Converting long int to string in PHP


I'm using FMS along with PHP and I need the client's ID in order to disconnect some user at some point. So, I retrieve client's ID from FMS, but FMS sends the ID as a long int, such as 4702111234508538223.

Here's my problem; I need to convert this number to something like oAACAAAA in PHP. Is there any short way or some kind of library exists to doing this? Otherwise I have to convert this AS3 library into PHP.


Solution

  • This function converts something like "4702111234525315439" into something like "oAADAAAA":

    function convert_id_to_str($id)
    {
       if (strspn($id, '0123456789') != strlen($id)) {
          return false;
       }
       $str = '';
       if (PHP_INT_SIZE >= 8) {
          while ($id) {
             $str .= chr($id & 255);
             $id >>= 8;
          }
       } else {
          while ($id) {
             $str .= chr(bcmod($id, '256'));
             $id   =     bcdiv($id, '256', 0);
          }
       }
       return $str;
    }