11

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);
}

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

2
  • look at unpack Commented Jul 31, 2013 at 6:30
  • @Orangepill I'm new in php and it is hard to dial with manual alone can you help me in a piece of code assume we start reading file with this code $filename = "myFile.sav"; $handle = fopen($filename, "rb"); $contents = fread($handle, filesize($filename)); Commented Jul 31, 2013 at 6:34

2 Answers 2

16

I believe the code you are looking for is:

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

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/>'; 
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Thanks Thanks Orangepill this is what i'm looking for. the code i'm use : $filename = "myFile.sav"; $handle = fopen($filename, "rb"); $fsize = filesize($filename); $contents = fread($handle, $fsize); $byteArray = unpack("N*",$contents); print_r($byteArray); for($n = 1; $n <= 16; $n++){ echo $byteArray [$n].'<br/>'; }
I could not get this to work. The example code fails at the unpack. Might be because of the file I'm attempting to read?
shouldn't it also be a good idead to call fclose on the file resource in the working example?
I found that this results in an array of 32-bit integers, so I'm not sure if the name $byteArray is appropriate. To get 8-bit integers, I had to use C* instead of N*
5

You should read the file as a string and then use unpack to parse it. In this case you have stored a 32-bit integer in big endian byte order, so use:

$out = unpack("N", $in);

The format codes are documented here: http://www.php.net/manual/en/function.pack.php

2 Comments

Thanks Joni but when in execute this code i get an array with one element only the solution below from Orangepill is so wonderfull and get all element in my file i'm use this code: ` $filename = "myFile.sav"; $handle = fopen($filename, "rb"); $fsize = filesize($filename); $contents = fread($handle, $fsize); $out = unpack("N", $contents); print_r($out); ` this code return one element only
If your file has packed ints all the way to the end and not just one you can use the asterix * formatting code to get them all. Or you can add a number to indicate how many ints should be read. This is all documented on the linked page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.