tags:

views:

43

answers:

1

Ok so i've got an array with integers (converted from intel Hex file), and need to output it as binary.

Here is the file reader, but how do i convert the array back to a byte stream (utf-8)?

$filename = "./latest/firmware.hex";
$file = fopen($filename, "r");
$image = array();
$imagesize = 0;
$count = 0;
$address = 0;
$type = 0;

while(!feof($file))
{
    $line = fgets($file);
    $count = intval(substr($line,1,2));
    $address = intval(substr($line,3,4));
    $type = intval(substr($line,7,2));
    if($type==0)
    {
        for ($i=0; $i < $count; $i++) { 
            $image[$address+1] = intval(substr($line,9+$i*2,2));
            if (($address + $i) > $imagesize)
            {
                $imagesize = $address + $i;
            }
        }
    }   
    else if($type == 1)
    {
        break;
    }
}
+2  A: 

You may want to collect it to a buffer before writing it to file. PHP strings can contain zeroes safely.

Notinlist
Additionally: simple `echo()` can write zero bytes to standard output. But it may have problems over 128 if you use UTF-8 as internal and/or output encoding.
Notinlist
I was a bit worried at first that this wasn't a UTF-8 solution but since chr() and fwrite() both treat it byte for byte you basically ignore the encoding, which means you should end up with a correct file.
Michael Clerx