note 50147 deleted from function.pack by googleguy

From: Date: Tue, 22 Oct 2013 10:15:13 +0000
Subject: note 50147 deleted from function.pack by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-196941@lists.php.net to get a copy of this message
Note Submitter: ---- 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.

« previous php.notes (#196941) next »