PHP 将文件读取为字节数组

2022-01-09 00:00:00 binary php

我有一个使用 JAVA 程序编写的文件,它有一个整数数组,写成二进制我在这个数组上创建了一个 for 循环并使用这个方法编写它

I have a file written using JAVA program which has an array of integers written as binary I made an for loop over this array and write it using this method

public static void writeInt(OutputStream out, int v) throws IOException {
    out.write((v >>> 24) & 0xFF);
    out.write((v >>> 16) & 0xFF);
    out.write((v >>> 8) & 0xFF);
    out.write((v >>> 0) & 0xFF);
}

我问的是如何在 PHP 中读取这个文件.

I'm ask about how to read this file in PHP.

推荐答案

相信你要找的代码是:

$byteArray = unpack("N*",file_get_contents($filename));

更新: OP 提供的工作代码

UPDATE: Working code supplied by OP

$filename = "myFile.sav"; 
$handle = fopen($filename, "rb"); 
$fsize = filesize($filename); 
$contents = fread($handle, $fsize); 
$byteArray = unpack("N*",$contents); 
print_r($byteArray); 
for($n = 0; $n < 16; $n++)
{ 
    echo $byteArray [$n].'<br/>'; 
}

相关文章