Find the answer to your Linux question:
Results 1 to 4 of 4
I can use hexedit to manually edit one or two hex values in a binary file, but if I want to change many of the, this is impractical. Is there ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2007
    Posts
    41

    Modifying hex values from a script


    I can use hexedit to manually edit one or two hex values in a binary file, but if I want to change many of the, this is impractical. Is there any tool that will take parameters like
    Code:
    someTool filename byteNumber replacementByte
    ?

    Thanks,

    David

  2. #2
    Just Joined!
    Join Date
    Sep 2006
    Location
    Norfolk Island
    Posts
    31
    You could try awk, it's bloody powerful, but I've never actually cracked it in any real sense..... Personally I would write a quick perl script.
    for a quick & dirty, have it move the file to .bak (or whatever), open the file, read binary char by char, output to the original filename & replace the char you want when it gets to it.
    This code has basics you need to proccess the files & deal with the hex chars. You can fill it out for full function:

    Code:
    open INFILE, "$infile";
    binmode (INFILE);
    open OUTFILE,">$outfile";
     binmode (OUTFILE);
    $replace=pack("H2",$hextext);
    while (read(INFILE, $buffer, 1)){
      $data = sprintf("%02X",ord($buffer));
      $charcount++;
      if ($charcount == $location){
         print OUTFILE $replace;
      }else{
         print OUTFILE pack("H2",$data);
      }
    }
    close INFILE;
    close OUTFILE;
    cheers

  3. #3
    Just Joined! VirtualLinuxUser's Avatar
    Join Date
    Mar 2008
    Location
    Pietermaritzburg, KwaZulu Natal, South Africa
    Posts
    28
    Ni_boy, is the script you've given in bash, PERL or AWK?

  4. #4
    Just Joined!
    Join Date
    Sep 2006
    Location
    Norfolk Island
    Posts
    31
    perl.
    obviously you'll need to do you own variable assignment, etc.
    Also, that's basically pulled from something I wrote where I was scanning files. If you wanted to replace a single byte in a specific loc there's probably a function for it in perl when writing binary mode, so the script could be a lot simpler.
    cheers

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •