Editing Binary Files

I’m trying to edit binary file while uploading to the server. My code opens file in binary mode, reads some bytes and does some mathematical operations. But I can’t find function that write binary data in binary form. fwrite(), fprintf(), sprintf() translate binary data into string and write their ASCII code. I tried chr(), ord(), bindec(), decbin(), hexdec(), dechex() and bin2hex() to convert string into binary format. Those functions work correctly while printing result on string. But when I try to write that result in a file, they automatically translates result into a string.

How can I stop translating binary data into string?

e.g. [code]$result = ord(‘1’) + ord(‘2’); /// 0x31 + 0x32

$fp = fopen(“sum.txt”, ‘wb’);
fprintf($fp, “%X”, $result);

This code writes 63 (0x36 0x33) in a file sum.txt.

What should I do so that answer 0x63 © will be written into file?

PS: I’m using PHP 5 on windows.

From the search results I got from Google, it seems that this is not a problem, but rather how PHP deals with binary formats. Can you tell us if the binary file has become corrupt or unusable after this operation?

Actually I’m doing this operation on locally stored files which aren’t binary files, but I’m trying to read them as binary files. e.g. If my file ‘A’ contains number ‘5’(0x35)in the file then it’s hex value should be return. Now suppose if want to double that value then it should be 0x70 and not ‘10’. But when I try to write 0x70 in file ‘B’, it becomes either 10 or 0x3730(70). Can you please tell me how can I write 0x70 in a file?

First of all: 0x35 * 2 = 0x6A, not 0x70. Secondly, you could take dechex() and its complement function for switching values between hexadecimal and decimal. The fact that 70 gets written as 0x3730 means that there is at least SOMETHING written in binary.

Dechex() converts decimal value to hexadecimal but while writting into file, it is treated as string and corresponding ASCII value gets written into file. I want to avoid this translation and write hex value as it is. I mean if I want to write value 0x36 in a file, then It should appear as ‘36’ in hex editor and 6 in notepad.

$a = 6; $fp = fopen("a.exe", "wb"); fwrite($fp, $a); fclose($fp);

This gives me a file containing 36 at location 0. I’m using the .exe extension to foce Textpad to open the file in Hex mode, but when I open it in notepad, it shows me a nice 6. Opening in ‘wb’ mode and writing a numeric value to it apparently saves the hex equivalent of this value. Could you see if this works?

Hello hmvartak, try to check out these links:


@Zyppora : Still you are not getting what I want to say. The code given by you works correctly because you know when you write string ‘6’ in file it’s ASCII value (0x36) gets writen in file. But what if want to write 0x90 or 0xAB. 0x36, 0x90 or 0xAB are the hex representations of bytes written in file. If you are not getting what I’m telling I’ll give you an example.
Consider a file structure of three bytes. When you open this file in hex editor it appears to be ‘3636 6C’. Third byte is sum of first two bytes. Now if I want to replace first byte with 37 then third byte must change to 6D. I want to write a program that do this task. Unfortunately when I add those first two bytes its answer is ‘3132’ whose corresponding string is ‘12’. If I used ord() function to add their ASCII values, answer is ‘6C’ but while writing in to file it is written as ‘3643’ which I want to avoid. I want to write a binary data in a file whose hex representation is ‘6C’.

@lordfrikk : Though your Second link is not telling what I’m asking, first one seemed to be quite helpful for reading dwords. Thanks for that.

Finally it worked. Here is my code.


<?PHP $fp = fopen("GTASA.B", 'rb'); $ptr = 0; $sum = 0; While($ptr < 202748) { fseek($fp, $ptr); $byte = fread($fp, 1); $ASCII = ord($byte); $sum = $sum + $ASCII; $ptr++; } fclose($fp); $pksum = pack("V", $sum); $fp = fopen("sum.txt", 'wb'); fwrite($fp, $pksum); fclose($fp); ?> [/code] This code reads file GTASA.B, make sum of all bytes in file and then write that sum as unsigned long in little endian byte order.

Thanks for all your help.

BTW I strongly recommend not using uppercase notation (e.g. While, <?PHP). 1. Looks lame. 2. It won’t be supported in future PHP versions.

Sponsor our Newsletter | Privacy Policy | Terms of Service