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 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:
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