"; echo $hexstr; $f = dirname(__FILE__) . "/ASCIIPHP.BIN"; CreateHexFile($f, $hexstr); echo "

DONE."; //////////////////////////////////////////////////////////////////// // USAGE: STRING = toHex(INTEGER) // This function converts an integer to a two-digit hexadecimal // number between 00 and FF. If the number is negative, the // function will return 00. If the number is 255 or greater, the // function will return FF. Anything in between will be converted // to a two-digit hexadecimal number. function toHex($c) { if ($c <= 0) return "00"; if ($c >= 255) return "ff"; return ($c < 16 ? "0" : "") . dechex($c | 0); } //////////////////////////////////////////////////////////////////// // USAGE: Status = CreateHexFile(FileName, HexString) // This function creates a binary file. The first argument must be // the name of the file. The second argument should be a string // that hold hexadecimal numbers. Each 2-digit number is // converted to a byte and written to the file. // Non-hex characters are ignored. // The function returns 1 on success or zero if an error occurred. function CreateHexFile($FileName, $Content) { $Status = 1; $Output = ""; // Remove all non-hexadecimal characters: $Content = preg_replace("/[^0-9a-fA-F]+/", "", $Content); // Make sure we have an even number of digits. if (strlen($Content) & 1) $Content .= "0"; // Convert hexadecimal numbers to bytes for ($i = 0; $i < strlen($Content); $i += 2) $Output .= chr(hexdec(substr($Content, $i, 2))); // Create file $f = fopen($FileName, "wb"); if (!fwrite($f, $Output)) $Status = 0; fclose($f); return $Status; } ?>