|
Re: Read and search through a binary file Robertico,
I'm no expert on Endianness but I understand the difference is in the order of the bytes, so maybe by reversing the $byteArray you get what you want. If not, I hope someone with more knowledge on Endianness provides the correct or a better method.
Try this:
$file = <file's path>
$pattern = '131B1B087C156108AE151B'
$prevBytes = 8
$bytes = [string]::join('', (gc $file -en byte | % {'{0:X2}' -f $_}))
[regex]::matches($bytes, $pattern, 1) |
% {
$i = $_.index - $prevBytes * 2
[string]::join('', $bytes[$i..($i + $prevBytes * 2 - 1)]) |
% {
$hexBytes = $_
$byteArray = 0..($hexBytes.length - 1) | ? {!($_ -band 1)} |
% {
$i = $_
"0x$($hexBytes.subString($i,2))"
}
[array]::reverse($byteArray)
[bitConverter]::toString($byteArray) -replace '-'
}
}
--
Kiron |