note 110107 added to function.fscanf
| From: | i at blackrabbit dot info | Date: | Wed, 19 Sep 2012 20:35:36 +0000 |
| Subject: | note 110107 added to function.fscanf | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-190887@lists.php.net to get a copy of this message | ||
I can't find out how to properly read an integer from a random place in a binary file, so I
wrote this function. It's a bicycle-style, and works only with unsigned, but it works for me:
function uintfread($fp, $intsize = 4, $endian = 'little')
{
if(!ftell($fp))
{
return false;
}
$result = 0;
$data = array();
for($i = 0; $i < $intsize; $i++)
{
$r = fread($fp, 1);
if($r == false)
{
$intsize = $i;
break;
}
$data[$i] = $r;
$data[$i] = ord($data[$i]);
}
if($endian == 'little')
{
$result = $data[$intsize-1];
if($intsize > 1)
{
for($i = $intsize-2; $i >= 0; $i--)
{
$result *= 256;
$result += $data[$i];
}
}
}
else
{
$result = $data[0];
if($intsize > 1)
{
for($i = 0; $i < $intsize; $i++)
{
$result *= 256;
$result += $data[$i];
}
}
}
return $result;
}
----
Server IP: 69.147.83.197
Probable Submitter: 77.235.104.41
----
Manual Page -- http://www.php.net/manual/en/function.fscanf.php
Edit -- https://master.php.net/note/edit/110107
Del: integrated -- https://master.php.net/note/delete/110107/integrated
Del: useless -- https://master.php.net/note/delete/110107/useless
Del: bad code -- https://master.php.net/note/delete/110107/bad+code
Del: spam -- https://master.php.net/note/delete/110107/spam
Del: non-english -- https://master.php.net/note/delete/110107/non-english
Del: in docs -- https://master.php.net/note/delete/110107/in+docs
Del: other reasons-- https://master.php.net/note/delete/110107
Reject -- https://master.php.net/note/reject/110107
Search -- https://master.php.net/manage/user-notes.php