Created
March 15, 2013 12:33
-
-
Save mems/5169578 to your computer and use it in GitHub Desktop.
Function to handle binayr string to write short, long, and array of shorts and longs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Decode a binary string (2 bytes only) to a short | |
* @param $input binary string to decode | |
* @return short decoded | |
*/ | |
function str2short($data) | |
{ | |
//*<- add or remove "/" to toggle comment | |
return (int)(ord($data[0]) << 8 | ord($data[1])); | |
/*/ | |
$ouput = unpack('n', $data); | |
return $output[1]; | |
//*/ | |
} | |
/** | |
* Decode a binary string to an array of short (n length, where n is length / 2 of binary string) | |
* If string length is odd, last byte isn't used | |
* @param $input binary string to decode | |
* @return array of short decoded | |
*/ | |
function str2multi_short($data) | |
{ | |
//*<- add or remove "/" to toggle comment | |
$output = array(); | |
$num = strlen($data); | |
for($i = 0; ($i + 1) < $num; $i+=2)//read 2 bytes | |
$output[] = (int)(ord($data[$i]) << 8 | ord($data[$i + 1])); | |
return $output; | |
/*/ | |
return array_merge(unpack('n*', $data)); | |
//*/ | |
} | |
/** | |
* Encode a short to a binary string (2 bytes) | |
* @param $input short to encode | |
* @return binary string contains short binary representation | |
*/ | |
function short2str($data) | |
{ | |
//*<- add or remove "/" to toggle comment | |
return chr($data >> 8) . chr($data & 0xFF); | |
/*/ | |
return pack('n', $data); | |
//*/ | |
} | |
/** | |
* Encode an array of short to a binary string (2 * n bytes length, where n is length of array) | |
* @param $input array of short to encode | |
* @return binary string contains shorts binary representation | |
*/ | |
function multi_short2str($data) | |
{ | |
//*<- add or remove "/" to toggle comment | |
$output = ''; | |
foreach ($data as $short) | |
$output .= chr($short >> 8) . chr($short & 0xFF); | |
return $output; | |
/*/ | |
return call_user_func_array(pack, array_merge(array('n*'), (array)$data)); | |
//*/ | |
} | |
/** | |
* Decode a binary string (4 bytes only) to a long | |
* @param $input binary string to decode | |
* @return long decoded | |
*/ | |
function str2long($data) | |
{ | |
//*<- add or remove "/" to toggle comment | |
return (int)(ord($data[0]) << 24 | ord($data[1]) << 16 | ord($data[2]) << 8 | ord($data[3])); | |
/*/ | |
$ouput = unpack('N', $data); | |
return $output[1]; | |
//*/ | |
} | |
/** | |
* Decode a binary string to an array of long (n length, where n is length / 4 of binary string) | |
* If string length haven't got a common denominator, remains bytes aren't used | |
* @param $input binary string to decode | |
* @return array of long decoded | |
*/ | |
function str2multi_long($data) | |
{ | |
//*<- add or remove "/" to toggle comment | |
$output = array(); | |
$num = strlen($data); | |
for($i = 0; ($i + 3) < $num; $i+=4)//read 2 bytes | |
$output[] = (int)(ord($data[$i]) << 24 | ord($data[$i + 1]) << 16 | ord($data[$i + 2]) << 8 | ord($data[$i + 3])); | |
return $output; | |
/*/ | |
return array_merge(unpack('n*', $data)); | |
//*/ | |
} | |
/** | |
* Encode a long to a binary string (4 bytes) | |
* @param $input long to encode | |
* @return binary string contains short binary representation | |
*/ | |
function long2str($data) | |
{ | |
//*<- add or remove "/" to toggle comment | |
return chr(($data >> 24) & 0xFF) . chr(($data >> 16) & 0xFF) . chr(($data >> 8) & 0xFF) . chr($data & 0xFF); | |
/*/ | |
return pack('n', $data); | |
//*/ | |
} | |
/** | |
* Encode an array of long to a binary string (4 * n bytes length, where n is length of array) | |
* @param $input array of long to encode | |
* @return binary string contains shorts binary representation | |
*/ | |
function multi_long2str($data) | |
{ | |
//*<- add or remove "/" to toggle comment | |
$output = ''; | |
foreach ($data as $long) | |
$output .= chr(($long >> 24) & 0xFF) . chr(($long >> 16) & 0xFF) . chr(($long >> 8) & 0xFF) . chr($long & 0xFF); | |
return $ouput; | |
/*/ | |
return call_user_func_array(pack, array_merge(array('n*'), (array)$data)); | |
//*/ | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment