View Single Post
Old 04-07-2008   #7 (permalink)
Robertico


 
 

Re: Read and search through a binary file

I needed to convert the returning values [string] to a little-endian value.

This doesn't work:
[bitconverter]::ToString([bitconverter]::GetBytes($out)) -replace '-',''
-or-
[bitconverter]::ToString([bitconverter]::GetBytes(0x$out)) -replace
'-',''

Appreciate some help.

Robertico


"Kiron" <Kiron@xxxxxx> wrote in message
news:BE8CE4C6-11F8-45A4-A8B7-B1839382A1CD@xxxxxx
Try this:

# v1
$file = <file's path>
$pattern = '131B1B087C156108AE151B'
$prevBytes = 8
$bytes = [string]::join('', (gc $file -en byte | % {'{0:x2}' -f $_}))
[regex]::matches($bytes, $pattern) |
% {
$i = $_.index - $prevBytes * 2
[string]::join('', $bytes[$i..($i + $prevBytes * 2 - 1)])
}

# v2 CTP
$file = <file's path>
$pattern = '131B1B087C156108AE151B'
$prevBytes = 8
$bytes = (gc $file -en byte | % {'{0:x2}' -f $_}) -join ''
select-string $pattern -inp $bytes -all |
% {$_.matches |
% {
$i = $_.index - $prevBytes * 2
$bytes[$i..($i + $prevBytes * 2 - 1)] -join ''
}
}

--
Kiron


My System SpecsSystem Spec