2

I have a 1024 bit long binary stream of data which I'd like to convert to an array of 32-bit integers (e.i. 32 numbers). From this question I used this code:

$filename = "myFile.sav"; 
$handle = fopen($filename, "rb"); 
$fsize = filesize($filename); 
$contents = fread($handle, $fsize); 
$byteArray = unpack("N*",$contents); 
print_r($byteArray); 

And even though it formats itself as "N", var_dump then prints out an array of 256 8-bit long integers. (I want 32 32-bit long numbers). What am I doing wrong?

EDIT: its not actually 256 8-bit numbers, but 256 gibberish values

1 Answer 1

1

It seems that your file contains the binary representation of your integers. Given any string representation of bits, the bindec function will convert them properly to integer:

$content = "1000110010110110100100101011000100100000101011010101101110101101011101110011001110010010111101001011111000111000101110011110011100110110000111001110000011001101011100111011110000001110111100100110110001000111111001010101100011100101010111000011010101010010001101101100011001101110001001000101110111011001001101111000110101010001101010000101110000100010000110110111000110001110000010000111001100100111110011000101000110100011111100100011110100101010101101011011101100111000101011110111111010001110000011101001011101111010101010011010011010011101100011111111000110000110000000000101110110010100010011010001110101101100110110001011010010010000011000111101110000100100001101100010101011000110010110110111100111010010101110000101011101010010101110100111100111110011000100001010111110010001010100001010001011101101110010011001010000101011110101100001100001111011101010100001001101100100110001101000000110111000111001100001000110000011011100000100011100100110101101000101111011110001100110010001111111010101110111111010110010111001";

$parts = str_split($content, 32);

for ($i = 0; $i < count($parts); ++$i) {
    $parts[$i] = bindec($parts[$i]);
}

print_r($parts);
Sign up to request clarification or add additional context in comments.

Comments

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.