Rafael
7/4/2008 3:40:00 PM
On 3 Jul., 02:52, Eric Hodel <drbr...@segment7.net> wrote:
> On Jul 2, 2008, at 03:02 AM, Rafael wrote:
>
>
>
> > On 1 Jul., 23:43, Eric Hodel <drbr...@segment7.net> wrote:
> >> On Jul 1, 2008, at 13:32 PM, Rafael wrote:
> >>> I'm trying to read a binary file and would want to find within the
> >>> file a particular substring. I have done the following
>
> >>> [...]
>
> >> unpack is useful when you need to turn binary data into usable
> >> structure. Just use a regular expression:
>
> >> open 'testfile', 'rb' do |io|
> >> if io.read(512) =~ /\020/ then
> >> puts "The tag was found"
> >> else
> >> puts "The tag was not found"
> >> end
> >> end
>
> > Hi,
>
> > thanks for your response, but it didn't solve my problem, so I guess I
> > didn't explain it correctly.
> > I'll give it another go.
>
> > I have a binary file.
>
> > What I want to do is to find the group 00201000 in the hexadecimal
> > representation. It's in the 11th line of output
> > 00000100 33 32 35 31 02 00 10 00 55 49 14 00 31 2e 32 2e |
> > 3251....UI..1.2.|
>
> you want to find the sequence of bytes "\0\020\010\0"?
>
> > if (strhex.include?("02001000")) == true then
>
> yes!
>
> the regex you want is: /\0\020\010\0/
>
> The big piece of code you wrote can be reduced to file.read(512) =~ /
> \0\020\010\0/. There's no need to convert all the text.
>
> Note that there is a bug in your code, it will also match on a half-
> byte:
>
> 00000100 33 32 35 31 02 00 10 00 55 49 14 00 31 2e 32 2e
> XXXXXXXX 33 32 35 30 20 01 00 01 55 49 14 00 31 2e 32 2e
> ^^^^^^^^^^^^
>
> The regex solution does not have this bug.
Hi,
thanks again for your message. You're absolutely right about the
bug ... how stupid of me!
But regarding the regular expression, shouldn't it be something like /
\x02\x00\x10\x00/ ?
Cheers
Rafael