Page 1 of 1

SOLVED: Writing array of hex values to file.

Posted: Fri Oct 16, 2009 9:31 pm
by Weiry
Currently i have an array of hex values (several hundred of them)

I am trying to open a file and write to the file as hex.
I have tried opening the file as a binary, which only inserts the array as a full string.

The other alternative i am trying at the moment, is using pack(), although i have never used this before.
Current code:

Code: Select all

$filename = 'phpHex.dat';
$Hex = $header.implode($Arr);
$HexArr = explode(" ",$Hex);
 
if (is_writable($filename)) {
    if (!$handle = fopen($filename, 'w+b')) {
         print "File: ($filename) can't be opened";
         exit;
    }
    $i=0;
    foreach($HexArr as $hex){
        //$index = 0x.$i;
        //fseek($handle, (binary)$i, SEEK_SET);
        if(!fwrite($handle, pack('H*', $hex))){
            print "Error writing to file";
        }
        //$i++;
    }
    fclose($handle);
}
All that returns is the error inside the IF statement.

Would appreciate any suggestions.

~Weiry

Re: Writing array of hex values to file.

Posted: Fri Oct 16, 2009 10:52 pm
by Weiry
Problem was fixed, i was trying to store an incorrect amount of hex data.
Problem was automatically fixed when i updated the amount of data being stored.

The code works fine with no changes.