Find And Replace 2-Byte Binary File

Welcome Forums General PowerShell Q&A Find And Replace 2-Byte Binary File

Viewing 6 reply threads
  • Author
    Posts
    • #275166
      Participant
      Topics: 3
      Replies: 11
      Points: 58
      Rank: Member

      hi, sorry for my bad English,

      I have this binary file,
      I open it with text/hex editor program (i using UltraEdit)
      and the result is hexadecimal like this:
      A7 9D DA 34 34 31 22 27 27 27 1E 1E 31 39 2E 18

      and I like to find and replace 2-byte type,
      just for example,  I like to find ‘ A7 9D‘ and replace it with ‘ DA 34‘,
      how  I do this with cmd or PowerShell?

      thank you for reading, have a nice day

    • #275217
      Senior Moderator
      Topics: 9
      Replies: 1449
      Points: 5,428
      Helping Hand
      Rank: Community MVP

      PowerShell do have a -replace operator which is regex based and should do the job for you.

      But I believe there should be some condition for replace action for your case. You have to code according to that.

    • #275229
      Participant
      Topics: 3
      Replies: 11
      Points: 58
      Rank: Member

      thank you for the answer, but the hexadecimal is not a string type,

      if like to know example data, you can try edit(open) with notepad a non-text data type like .exe, . jpg,  .mp4,  etc, and the result will similar to this:

      $! !( $à$  ¯  ¯šJ   ¯øì  $ýÿ@ 9 <© èå&Î ! ì ! $(ƒ¯ïQ ð $! !( !0 @$  ±¯4&! @!(

      note: please dont open above 1mb file, the notepad will “not responding”.

      and now, try open it with HexEditor, you will understand what i talking about

    • #275232
      Senior Moderator
      Topics: 9
      Replies: 1449
      Points: 5,428
      Helping Hand
      Rank: Community MVP

      but the hexadecimal is not a string type,

      Any hex data can be considered as a string when wrapped in quotes, isn’t it ? are you dealing with special chars ?

    • #275235
      Participant
      Topics: 3
      Replies: 11
      Points: 58
      Rank: Member

      i dont know that, sorry, newbie here,

      i tried your code and do not work

      PowerShell -ExecutionPolicy Bypass -C "(Get-Content 'C:\Games\New folder (4)\input.bin' -raw).replace('50 53','20 23')

      can you provide working code?,

      Edit, i forgot to put output file:

      PowerShell -ExecutionPolicy Bypass -C "(Get-Content 'C:\Games\New folder (4)\input.bin' -raw) -replace('50', '20') | Set-Content 'C:\Games\New folder (4)\input2.bin'

      Still not work

    • #275268
      Participant
      Topics: 4
      Replies: 424
      Points: 744
      Helping Hand
      Rank: Major Contributor

      Something like this should work.  I tested it on a text file so it’s easy to see the result but it should work the same for binary files.

      The text file just contained the word ‘hello’, and the code replaces the first two letters.

       

    • #275454
      Participant
      Topics: 3
      Replies: 11
      Points: 58
      Rank: Member

      @Matt Bloomfield

      Thank you Thank you Thank you, The Code is work,

      and sorry for late reply, i just wake up, i live in indonesia, now 6.34-AM

Viewing 6 reply threads
  • You must be logged in to reply to this topic.