Reading a binary using PHP

This is the used C ++ structure.

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 contents of the binary generated when compiling with the 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 charges 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 charges 92 16.12 01.01.2000 z 12.23 34.12 90.12 charges 12 56.12 31 / 12/2010 a us 52.23 54.12 10.12 charges 92 16.12 12/31/2010 d us 12.23 34.12 90.12 charges 12 56.12

PHP encoding to read the contents of the specified binary.

 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 get is a lot of garbage values ​​in the second and seventh field. And the accuracy exceeds the float field. How to fix it?

12/12/1995 d¸³M · g · ew · ¤dw · àew · Öúr · ÿÿÿÿ, w · ø; w · ̪¹¿ô {· {· <ây · {{· SpP · ip · ¤dw · EW · € 5w · OOG · 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

01/01/1998 a¸³M · g · ew · ¤dw · àew · Öúr · ÿÿÿÿ, w · ø; w · ̪¹¿ô {· {· <ây · {{· SpP · ip · ¤dw · EW · € 5w · OOG · X "¹¿" qP · äSg us 52.229999542236 54.119998931885 10.119999885559 fees · 5rP · # Ä ä SS ô Å Å Ä Ä 5rP · 0 # o · ¸xw · x "¹¿ · ÿÿÿÿýŽäSg · ¸xw · ÆD¬¹¿ L¬¹À5rP · pòy · 92 16.120000839233

12/31/1999 a¸³M · g · ew · ¤dw · àew · Öúr · ÿÿÿÿ, w · ø; w · ̪¹¿ô {· {· <ây · {{· SpP · ip · ¤dw · EW · € 5w · OOG · X "¹¿" qP · äSg us 52.229999542236 54.119998931885 10.119999885559 fees · 5rP · # Ä ä SS ô Å Å Ä Ä 5rP · 0 # o · ¸xw · x "¹¿ · ÿÿÿÿýŽäSg · ¸xw · ÆD¬¹¿ L¬¹À5rP · pòy · 92 16.120000839233

12/31/1999 d¸³M · g · ew · ¤dw · àew · Öúr · ÿÿÿÿ, w · ø; w · ̪¹¿ô {· {· <ây · {{· SpP · ip · ¤dw · EW · € 5w · OOG · 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

01/01/2000 a¸³M · g · ew · ¤dw · àew · Öúr · ÿÿÿÿ, w · ø; w · ̪¹¿ô {· {· <ây · {{· SpP · ip · ¤dw · EW · € 5w · OOG · X "¹¿" qP · äSg us 52.229999542236 54.119998931885 10.119999885559 fees · 5rP · # Ä ä SS ô Å Å Ä Ä 5rP · 0 # o · ¸xw · x "¹¿ · ÿÿÿÿýŽäSg · ¸xw · ÆD¬¹¿ L¬¹À5rP · pòy · 92 16.120000839233

01/01/2000 z¸³M · g · ew · ¤dw · àew · Öúr · ÿÿÿÿ, w · ø; w · ̪¹¿ô {· {· <ây · {{· SpP · ip · ¤dw · EW · € 5w · OOG · 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

12/31/2010 a¸³M · g · ew · ¤dw · àew · Öúr · ÿÿÿÿ, w · ø; w · ̪¹¿ô {· {· <ây · {{· SpP · ip · ¤dw · EW · € 5w · OOG · X "¹¿" qP · äSg us 52.229999542236 54.119998931885 10.119999885559 fees · 5rP · # Ä ä SS ô Å Å Ä Ä 5rP · 0 # o · ¸xw · x "¹¿ · ÿÿÿÿýŽäSg · ¸xw · ÆD¬¹¿ L¬¹À5rP · pòy · 92 16.120000839233

12/31/2010 d¸³M · g · ew · ¤dw · àew · Öúr · ÿÿÿÿ, w · ø; w · ̪¹¿ô {· {· <ây · {{· SpP · ip · ¤dw · EW · € 5w · OOG · 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

+4
source share
2 answers

In PHP pack and unpack the formatting code a means NUL padded string. In this case, it seems that the second and seventh fields are not filled with NUL; they just have a NUL to indicate where the line ends, followed by random data.

To get the portion of the string before NUL, you can use substr and strpos :

 $input = "a\000b"; // string with embedded NUL $output = substr($input, 0, strpos($input, "\000")); var_dump($output); // string(1) "a" 

Floating-point fields have the correct values, the values ​​that you see at the exit from program C are rounded. To do the same in PHP, you can use sprintf , for example:

 $input = 16.120000839233; $output = sprintf("%.2f", $input); var_dump($output); // string(5) "16.12" 
0
source

Yes, because your second argument for unpacking is “a128ac” and manual (http://jp2.php.net/manual/en/function.pack.php), says “a” means “NUL-padded string” but you probably want "A" which is a "SPACE-padded string". What you see is garbage because the terminator was not found properly.

0
source

All Articles