Find And Replace 2-Byte Binary File

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

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

'A7 9D DA 34 34 31 22 27 27 27 1E 1E 31 39 2E 18' -replace 'A7 9D','DA 34'

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

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 @$1ø ±¯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

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

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

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.

#Read in the text file as bytes
$bytes = Get-Content E:\Temp\filea.txt -Encoding Byte -ReadCount 0

#Convert the bytes to hex
$hexString = [System.BitConverter]::ToString($bytes)

#Replace 'h' and 'e' with 'f' and 'g'
$newHexString = $hexString -replace '68-65','66-67'

#Convert the hex back to bytes
$newBytes = $newHexString.Split('-') | foreach {[byte]::Parse($_, 'hex')}

#Update the contents of the file
$newBytes | Set-Content E:\Temp\filea.txt -Encoding Byte


@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