Ich hab mich hier mit „unpack“ noch einmal mehrere Stunden beschäftigt, aber irgendwo habe ich ein Problem bzw. einen Fehler …
Zum Testen habe ich jetzt mal folgendes getestet:
Test 1 - mit „\xFF\xFF\xFE\x3C“
<?
$binarydata = "\xFF\xFF\xFE\x3C";
error_reporting(0);
// http://php.net/manual/de/function.pack.php
$formatting = array("a","A","h","H","c","C","s","S","n","v","i","I","l","L","N","V","q","Q","J","P","f","g","G","d","e","E","x","X","Z","@");
foreach($formatting as $format) {
$value = unpack($format, $binarydata);
echo "unpack format '".$format."' > ";
$result = $value[1];
$lastError = error_get_last();
if($lastError != null) {
echo "ERROR/WARNING: " . $lastError[message] . "
";
} else {
var_dump($result);
}
}
?>
Ergebnis ist:
unpack format 'a' > string(1) "ÿ"
unpack format 'A' > string(1) "ÿ"
unpack format 'h' > string(1) "f"
unpack format 'H' > string(1) "f"
unpack format 'c' > int(-1)
unpack format 'C' > int(255)
unpack format 's' > int(-1)
unpack format 'S' > int(65535)
unpack format 'n' > int(65535)
unpack format 'v' > int(65535)
unpack format 'i' > int(1023344639)
unpack format 'I' > int(1023344639)
unpack format 'l' > int(1023344639)
unpack format 'L' > int(1023344639)
unpack format 'N' > int(4294966844)
unpack format 'V' > int(1023344639)
unpack format 'q' > ERROR/WARNING: unpack(): Type q: not enough input, need 8, have 4
unpack format 'Q' > ERROR/WARNING: unpack(): Type Q: not enough input, need 8, have 4
unpack format 'J' > ERROR/WARNING: unpack(): Type J: not enough input, need 8, have 4
unpack format 'P' > ERROR/WARNING: unpack(): Type P: not enough input, need 8, have 4
unpack format 'f' > ERROR/WARNING: Use of undefined constant message - assumed 'message' (this will throw an Error in a future version of PHP)
unpack format 'g' > ERROR/WARNING: Use of undefined constant message - assumed 'message' (this will throw an Error in a future version of PHP)
unpack format 'G' > ERROR/WARNING: Use of undefined constant message - assumed 'message' (this will throw an Error in a future version of PHP)
unpack format 'd' > ERROR/WARNING: unpack(): Type d: not enough input, need 8, have 4
unpack format 'e' > ERROR/WARNING: unpack(): Type e: not enough input, need 8, have 4
unpack format 'E' > ERROR/WARNING: unpack(): Type E: not enough input, need 8, have 4
unpack format 'x' > ERROR/WARNING: Undefined offset: 1
unpack format 'X' > ERROR/WARNING: Undefined offset: 1
unpack format 'Z' > ERROR/WARNING: Use of undefined constant message - assumed 'message' (this will throw an Error in a future version of PHP)
unpack format '@' > ERROR/WARNING: Undefined offset: 1
Test 2 - mit „\xFF\xFF\xFF\xFF\xFF\xFF\xFE\x3C“
<?
$binarydata = "\xFF\xFF\xFF\xFF\xFF\xFF\xFE\x3C";
error_reporting(0);
// http://php.net/manual/de/function.pack.php
$formatting = array("a","A","h","H","c","C","s","S","n","v","i","I","l","L","N","V","q","Q","J","P","f","g","G","d","e","E","x","X","Z","@");
foreach($formatting as $format) {
$value = unpack($format, $binarydata);
echo "unpack format '".$format."' > ";
$result = $value[1];
$lastError = error_get_last();
if($lastError != null) {
echo "ERROR/WARNING: " . $lastError[message] . "
";
} else {
var_dump($result);
}
}
?>
Ergebnis ist:
unpack format 'a' > string(1) "ÿ"
unpack format 'A' > string(1) "ÿ"
unpack format 'h' > string(1) "f"
unpack format 'H' > string(1) "f"
unpack format 'c' > int(-1)
unpack format 'C' > int(255)
unpack format 's' > int(-1)
unpack format 'S' > int(65535)
unpack format 'n' > int(65535)
unpack format 'v' > int(65535)
unpack format 'i' > int(-1)
unpack format 'I' > int(4294967295)
unpack format 'l' > int(-1)
unpack format 'L' > int(4294967295)
unpack format 'N' > int(4294967295)
unpack format 'V' > int(4294967295)
unpack format 'q' > int(4395231761336893439)
unpack format 'Q' > int(4395231761336893439)
unpack format 'J' > int(-452)
unpack format 'P' > int(4395231761336893439)
unpack format 'f' > float(NAN)
unpack format 'g' > float(NAN)
unpack format 'G' > float(NAN)
unpack format 'd' > float(6,883382752676E-15)
unpack format 'e' > float(6,883382752676E-15)
unpack format 'E' > float(NAN)
unpack format 'x' > ERROR/WARNING: Undefined offset: 1
unpack format 'X' > ERROR/WARNING: Undefined offset: 1
unpack format 'Z' > ERROR/WARNING: Use of undefined constant message - assumed 'message' (this will throw an Error in a future version of PHP)
unpack format '@' > ERROR/WARNING: Undefined offset: 1
Komisch:
- Beim Test1 kommt nirgends das erwartetet Ergebnis von -452 aus!
- Beim Test2 kommt ausgerechnet beim Formatzeichen ‚J‘ das Ergebnis -452 raus !??
‚J‘ ist aber laut PHP Doku ein „vorzeichenloser Long-Long-Typ (immer 64 bit, Byte-Folge Big Endian)“
Habt ihr eine Idee wo ich da was falsch mache oder falsch sehe …
Note: Ich verwende dabei IP-Symcon v5.0 auf Windows 10 Enterprise 64-Bit (mit 16GB RAM)
