Hex editting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aquadeluxe
Forum Newbie
Posts: 6
Joined: Mon Mar 31, 2008 7:18 pm

Hex editting

Post by aquadeluxe »

Is there anyway in PHP to change hex values like in C?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Hex editting

Post by Christopher »

(#10850)
aquadeluxe
Forum Newbie
Posts: 6
Joined: Mon Mar 31, 2008 7:18 pm

Re: Hex editting

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Hex editting

Post 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
aquadeluxe
Forum Newbie
Posts: 6
Joined: Mon Mar 31, 2008 7:18 pm

Re: Hex editting

Post 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.
Post Reply