<?php

/* fread / fwrite are noted as 'binary safe' - however, it appears
 * impossible to use them in this fashion because of automatic type
 * casting to a string...
 *
 * in this example - 0x0c is converted and written as a string '12'..
 * then fread, rather than return 0x31, converts it again.
 *
 * do you think we need two dedicated binary read/write functions?  new
 * arguments?  or did I miss something...
 *
 * see: ext/standard/file.c - fread(), fwrite().
 */

define(TEST_FILE, "test.out");
$binary_value = 0x0c;

echo gettype($binary_value) . "\n";
printf("0x%02x\n", $binary_value);

$fh = fopen(TEST_FILE, "wb");		/* b is ignored under UNIX */
fwrite($fh, $binary_value);		/* always converts to string... */
fclose($fh);


$fh = fopen(TEST_FILE, "rb");
$value = fread($fh, 1);			/* the same applies to fread.. */
fclose($fh);

echo gettype($value) . "\n";
printf("0x%02x\n", $value);
printf("0x%02x\n", (int) $value);	/* cast string to int */

?>
