Page 1 of 1

Hex editting

Posted: Mon Mar 31, 2008 7:34 pm
by aquadeluxe
Is there anyway in PHP to change hex values like in C?

Re: Hex editting

Posted: Mon Mar 31, 2008 11:33 pm
by Christopher

Re: Hex editting

Posted: Tue Apr 01, 2008 3:41 am
by aquadeluxe
Sorry I didn't explain myself well enough. I wanted to know how I can edit hex values from a file. An example would be change the offset 0x103 from 74 to 6E.

Re: Hex editting

Posted: Tue Apr 01, 2008 3:46 am
by Chris Corbyn
aquadeluxe wrote:Sorry I didn't explain myself well enough. I wanted to know how I can edit hex values from a file. An example would be change the offset 0x103 from 74 to 6E.
Untested:

Code: Select all

$fp = fopen($path_to_file, "w+b"); //Open file for reading and writing in binary mode
fseek($fp, 0x103, SEEK_SET); //Seek to 0x103
fwrite($fp, pack('H*', '6E')); //Write 0x6E packed into a binary byte
fclose($fp); //Close the file
Thing to check the manual for:

http://php.net/fopen
http://php.net/fread
http://php.net/fwrite
http://php.net/fseek
http://php.net/fclose
http://php.net/pack
http://php.net/unpack

Re: Hex editting

Posted: Tue Apr 01, 2008 4:56 am
by aquadeluxe
It worked, kinda. It change 0x103 to 6E, but everything before it was 00 and the file ended at 0x103 instead of keep on going.