This is the ‘C++’ Structure used.
struct gross
{
char date[11];
char ac[128];
char type[5];
float mvalue;
float netraw;
float netfer;
char stat[128];
float firr;
float acb;
};
This is the content of binary file generated when compiled with GCC compiler.
12/12/1995 d us 12.23 34.12 90.12 fees 12 56.12
01/01/1998 a us 52.23 54.12 10.12 fees 92 16.12
31/12/1999 a us 52.23 54.12 10.12 fees 92 16.12
31/12/1999 d us 12.23 34.12 90.12 fees 12 56.12
01/01/2000 a us 52.23 54.12 10.12 fees 92 16.12
01/01/2000 z us 12.23 34.12 90.12 fees 12 56.12
31/12/2010 a us 52.23 54.12 10.12 fees 92 16.12
31/12/2010 d us 12.23 34.12 90.12 fees 12 56.12
The PHP coding to read the content of the above binary file.
echo "<table>";
while (!feof($f)) {
if ($s = fread($f, 292)) {
$nn = unpack('a11date/a128ac/a5type/fmvalue/fnetraw/fnetfer/a128stat/ffirr/facb', $s);
echo "<td>" . $nn[date] ."</td>";
echo "<td>" . $nn[ac] . "</td>";
echo "<td>" . $nn[type] . "</td>";
echo "<td>" . $nn[mvalue] . "</td>";
echo "<td>" . $nn[netraw] . "</td>";
echo "<td>" . $nn[netfer] . "</td>";
echo "<td>" . $nn[stat] . "</td>";
echo "<td>" . $nn[firr] . "</td>";
echo "<td>" . $nn[acb] . "</td>";
echo "</tr>";
}
}
echo "</table>";
fclose($f);
?>
This is what I got from the above code. What I’m getting is lot of garbage values in the second and seventh field. And the precision exceeds for the float fields. How to fix this?
12/12/1995 d¸³M·g·ew·¤dw·àew·Öúr·ÿÿÿÿ,w·ø;w·Ìª¹¿ô{·{·‹ây·Ð{·SpP·ip·¤dw·ew·€5w·ôOg·
X«¹¿»qP·äSg us 12.229999542236 34.119998931885 90.120002746582 fees·5rP· #Ä äSg· rP·ô›Å #Ä 5rP·0#o·¸xw·x«¹¿·ÅÿÿýŽäSg·¸xw·ÆD¬¹¿L¬¹¿5rP·pòy· 12 56.11999893188501/01/1998 a¸³M·g·ew·¤dw·àew·Öúr·ÿÿÿÿ,w·ø;w·Ìª¹¿ô{·{·‹ây·Ð{·SpP·ip·¤dw·ew·€5w·ôOg·
X«¹¿»qP·äSg us 52.229999542236 54.119998931885 10.119999885559 fees·5rP· #Ä äSg· rP·ô›Å #Ä 5rP·0#o·¸xw·x«¹¿·ÅÿÿýŽäSg·¸xw·ÆD¬¹¿L¬¹¿5rP·pòy· 92 16.12000083923331/12/1999 a¸³M·g·ew·¤dw·àew·Öúr·ÿÿÿÿ,w·ø;w·Ìª¹¿ô{·{·‹ây·Ð{·SpP·ip·¤dw·ew·€5w·ôOg·
X«¹¿»qP·äSg us 52.229999542236 54.119998931885 10.119999885559 fees·5rP· #Ä äSg· rP·ô›Å #Ä 5rP·0#o·¸xw·x«¹¿·ÅÿÿýŽäSg·¸xw·ÆD¬¹¿L¬¹¿5rP·pòy· 92 16.12000083923331/12/1999 d¸³M·g·ew·¤dw·àew·Öúr·ÿÿÿÿ,w·ø;w·Ìª¹¿ô{·{·‹ây·Ð{·SpP·ip·¤dw·ew·€5w·ôOg·
X«¹¿»qP·äSg us 12.229999542236 34.119998931885 90.120002746582 fees·5rP· #Ä äSg· rP·ô›Å #Ä 5rP·0#o·¸xw·x«¹¿·ÅÿÿýŽäSg·¸xw·ÆD¬¹¿L¬¹¿5rP·pòy· 12 56.11999893188501/01/2000 a¸³M·g·ew·¤dw·àew·Öúr·ÿÿÿÿ,w·ø;w·Ìª¹¿ô{·{·‹ây·Ð{·SpP·ip·¤dw·ew·€5w·ôOg·
X«¹¿»qP·äSg us 52.229999542236 54.119998931885 10.119999885559 fees·5rP· #Ä äSg· rP·ô›Å #Ä 5rP·0#o·¸xw·x«¹¿·ÅÿÿýŽäSg·¸xw·ÆD¬¹¿L¬¹¿5rP·pòy· 92 16.12000083923301/01/2000 z¸³M·g·ew·¤dw·àew·Öúr·ÿÿÿÿ,w·ø;w·Ìª¹¿ô{·{·‹ây·Ð{·SpP·ip·¤dw·ew·€5w·ôOg·
X«¹¿»qP·äSg us 12.229999542236 34.119998931885 90.120002746582 fees·5rP· #Ä äSg· rP·ô›Å #Ä 5rP·0#o·¸xw·x«¹¿·ÅÿÿýŽäSg·¸xw·ÆD¬¹¿L¬¹¿5rP·pòy· 12 56.11999893188531/12/2010 a¸³M·g·ew·¤dw·àew·Öúr·ÿÿÿÿ,w·ø;w·Ìª¹¿ô{·{·‹ây·Ð{·SpP·ip·¤dw·ew·€5w·ôOg·
X«¹¿»qP·äSg us 52.229999542236 54.119998931885 10.119999885559 fees·5rP· #Ä äSg· rP·ô›Å #Ä 5rP·0#o·¸xw·x«¹¿·ÅÿÿýŽäSg·¸xw·ÆD¬¹¿L¬¹¿5rP·pòy· 92 16.12000083923331/12/2010 d¸³M·g·ew·¤dw·àew·Öúr·ÿÿÿÿ,w·ø;w·Ìª¹¿ô{·{·‹ây·Ð{·SpP·ip·¤dw·ew·€5w·ôOg·
X«¹¿»qP·äSg us 12.229999542236 34.119998931885 90.120002746582 fees·5rP· #Ä äSg· rP·ô›Å #Ä 5rP·0#o·¸xw·x«¹¿·ÅÿÿýŽäSg·¸xw·ÆD¬¹¿L¬¹¿5rP·pòy· 12 56.119998931885
In PHP’s
packandunpack, the formatting codeastands for NUL padded string. In this case it looks like the second and seventh field are not NUL padded; they just have NUL to indicate where the string ends, followed by random data.To get the part of the string before NUL you can use
substrandstrpos:The float fields have their correct values, the values you see in the output from the C program are rounded. To do the same in PHP you could use
sprintffor example: