[PHP] 處理二進制資料 – Binary Data Pack/Unpack

Intro

PHP支援二進制的資料處理,應用上在跟低階語言(如C語言)做資料介接就幫上許多。

PHP函式

pack() 封裝 Data to BIN
unpack 分析 BIN to Data

$bin = pack("Sa3", 50, 'abc');
file_put_contents('data.bin', $bin);
$bin = file_get_contents('data.bin');
$data = unpack("SShort/a3Char3", $bin);
print_r($data);
Array
(
    [Short] => 50
    [Char[3]] => abc
)

Leave a Reply

Your email address will not be published. Required fields are marked *