Using fwrite to write an int (BINARY) to a file
Posted: Fri Feb 14, 2003 3:42 pm
If I code up the following:
This prints out:
Writing 0x4b to the file
Unfortunately, when I open the file, PHP has converted the binary value to an ASCII string representing the decimal value before writing to the file, so the file actually contains two bytes: 0x36 0x39, or "69" if viewed from a text editor.
I have added the optional size arg to fwrite ( fwrite($fp, $val, 1); ), but the file ends up with only 0x36 ("6"), so the string conversion is still taking place.
I don't want this string conversion to take place. I want a single byte (0x4b) in a binary file.
How can I accomplish this?
Thanks in advance.
-Marc
Code: Select all
$val = 0x4b; // dec 69
$fp = fopen($binfile, "wb");
printf("Writing 0x%x to the file", $val);
fwrite($fp, $val);Writing 0x4b to the file
Unfortunately, when I open the file, PHP has converted the binary value to an ASCII string representing the decimal value before writing to the file, so the file actually contains two bytes: 0x36 0x39, or "69" if viewed from a text editor.
I have added the optional size arg to fwrite ( fwrite($fp, $val, 1); ), but the file ends up with only 0x36 ("6"), so the string conversion is still taking place.
I don't want this string conversion to take place. I want a single byte (0x4b) in a binary file.
How can I accomplish this?
Thanks in advance.
-Marc