Skip to content

Instantly share code, notes, and snippets.

@segrax
Last active August 29, 2015 14:25
Show Gist options
  • Save segrax/520ff956ccc18cf99d0b to your computer and use it in GitHub Desktop.
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)
<?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