note 50147 added to function.pack

From: Date: Sat, 19 Feb 2005 19:09:56 +0000
Subject: note 50147 added to function.pack
Groups: php.notes 
Request: Send a blank email to php-notes+get-85254@lists.php.net to get a copy of this message
I needed to convert binary values from a file to integers. Maybe there is something simpler, but the snippets i saw above seemed a little convoluted: function bin2asc ($binary) { $val = 0; for ($i = strlen($binary) - 1; $i >= 0; $i--) { $ch = substr($binary, $i, 1); $val = ($val << 8) | ord($ch); } return $val; } This was called like the following from a binary file: function _readInt($fp) { return bin2asc(fread($fp, 4)); } Note that the for loop should be reversed for network byte order instead of intel byte order. Also the conversion will work with any number of bytes, but will happily overflow. ---- Manual Page -- http://www.php.net/manual/en/function.pack.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+50147 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50147&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50147&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50147&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50147&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50147&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+50147&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#85254) next »