PHP Array to binary data
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;
}
}
Step 1: Use
chr()
to get character from ascii value.Step 2: Use
fwrite()
to write binary data to file.
You may want to collect it to a buffer before writing it to file. PHP strings can contain zeroes safely.
精彩评论