View Full Version : Can I write hex to a binary file?

11-24-2003, 07:34 AM
I need to write hex to a binary file. But the functions fputs() and fwrite() only seem to be able to write in strings. If I want to write 064 to the file, I want it to show up as @ (1 byte) not "064" (3 bytes). A test page for the hex output is at http://scorpius.spaceports.com/~xfox/s3kc/edit/php.htm. Is there a function to unescape hex values upon write?

11-24-2003, 03:53 PM
quote from the manual

Note: On systems which differentiate between binary and text files (i.e. Windows) the file must be opened with 'b' included in fopen() mode parameter.

Did you open the file in binary mode?

11-24-2003, 04:57 PM
Originally posted by Spookster
quote from the manual

Did you open the file in binary mode?

Yes, I opened it in binary.


But good does that do when you can only write strings? If would like to do something like the following:

fwrite($fr,%1e); // writing hex bytes into the file.
//(where % means the hex code for a specific byte)

That should be specific enough. But if a file is opened in binary, how does that differentiate from any other type of opening? I only know how FTP treats binary and ascii different.