Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Read and search through a binary file

Reply
 
Old 04-08-2008   #11 (permalink)
Keith Hill [MVP]


 
 

Re: Read and search through a binary file

"Robertico" <Nomail@xxxxxx> wrote in message
news:eJZVatZmIHA.5084@xxxxxx
Quote:

> Kiron,
>
> Thanks for the explanation. I'am running version 1.0.
> I didn't notice the meaning off v1 and v2. I thought that is was just an
> other approach.
>
> I need to convert the returning 'prevoius bytes' from "Big Endian" to
> 'Little Endian'' . The binary file is in "Big Endian".
> The code " [bitConverter]::toString($byteArray) -replace '-' " indeed
> doesn't convert to "Little Endian".
> So i need some advice to fix this.
>
Take a look at [System.Net.IPAddress]::NetworkToHostOrder(). Assuming you
are running on an x86 based platform, this will convert big endian to little
endian.

--
Keith


My System SpecsSystem Spec
Old 04-08-2008   #12 (permalink)
Kiron


 
 

Re: Read and search through a binary file

Another attempt, this time using the System.Text.Encoding Class to convert each [byte[]] from BigEndian encoding to LittleEndian encoding.

$file = <file's path>
$pattern = '131B1B087C156108AE151B'
$prevBytes = 8
$bytes = [string]::join('', (gc $file -en byte | % {'{0:X2}' -f $_}))
$bigEndian = [text.encoding]::bigEndianUnicode
$littleEndian = [text.encoding]::unicode
[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))"
}
$littleEndianBytes = [text.encoding]::convert($bigEndian, $littleEndian, $byteArray)
[string]::join('', ($littleEndianBytes | % {'{0:X2}' -f $_}))
# next line produces same output as the previous line
# [bitConverter]::toString($littleEndianBytes) -replace '-'
}
}

--
Kiron
My System SpecsSystem Spec
Old 04-10-2008   #13 (permalink)
Robertico


 
 

Re: Read and search through a binary file

Krion,

This one does the trick :-))
e.g. the return value in Big Endian is '097AD6060956354F' en after the
conversion to Little Endian it becomes '4F35560906D67A09'.

Thanks a lot !

Now it's working and time to study why it's working :-))

Robertico



"Kiron" <Kiron@xxxxxx> wrote in message
news:C4D22AE4-2DCE-426E-B53B-77E2A3D4B68E@xxxxxx
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


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Binary search and replace General Discussion
Re: Altering a binary file on the fly .NET General
writing an binary file from sql to the disk with vb.net .NET General
search and replace in binary file VB Script
How to embed manifest in TCL binary? - mt.exe corrupting my binary Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46