Last active
August 29, 2015 14:25
-
-
Save segrax/520ff956ccc18cf99d0b to your computer and use it in GitHub Desktop.
Take an array of bytes, and output a series of signed integers (16bit)
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 | |
$data = array( | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 | |
); | |
$final = array(); | |
function zeropad($num, $lim) { | |
return (strlen($num) >= $lim) ? $num : zeropad("0" . $num, $lim); | |
} | |
do { | |
$piece = zeropad((string) dechex(array_shift( $data )),2); | |
$piece = zeropad(((string) dechex(array_shift($data))),2). $piece; | |
$final[] = $piece; | |
} while( count( $data )); | |
$results = array(); | |
foreach( $final as $piece ) { | |
$results[] = unpack("s", pack("s", hexdec( $piece ))); | |
} | |
$count = 0; | |
foreach( $results as $result ) { | |
echo $result[1] . ", "; | |
++$count; | |
if($count==10) { | |
echo "\n"; | |
$count = 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment