note 114359 added to function.unpack

From: Date: Wed, 12 Feb 2014 13:25:27 +0000
Subject: note 114359 added to function.unpack
Groups: php.notes 
Request: Send a blank email to php-notes+get-198467@lists.php.net to get a copy of this message
I had trouble finding anyone who could explicitly say how to convert a varbinary (SQL Server) database value into a array of signed integers, e.g. for a waveform. In SQL Server varbinary stores big-endian signed shorts. To pull this out I used the following: <?php $unsignedWaveform = unpack('n*', $binaryWaveform); $waveform = array_map('unsignedToSignedShort', $unsignedWaveform); function unsignedToSignedShort($short) { if ($short >= pow(2, 15)) { return $short -= pow(2, 16); } else { return $short; } } ?> The n* was the magic that splits the large binary into individual shorts in an array. ---- Server IP: 72.52.91.14 Probable Submitter: 72.52.91.14 ---- Manual Page -- http://php.net/manual/en/function.unpack.php Edit -- https://master.php.net/note/edit/114359 Del: integrated -- https://master.php.net/note/delete/114359/integrated Del: useless -- https://master.php.net/note/delete/114359/useless Del: bad code -- https://master.php.net/note/delete/114359/bad+code Del: spam -- https://master.php.net/note/delete/114359/spam Del: non-english -- https://master.php.net/note/delete/114359/non-english Del: in docs -- https://master.php.net/note/delete/114359/in+docs Del: other reasons-- https://master.php.net/note/delete/114359 Reject -- https://master.php.net/note/reject/114359 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#198467) next »