[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to search for ascii char sequence in binary file

Earle Clubb

10/23/2007 2:59:00 PM

So I'm trying to convert e.g. 'RIFF' to e.g. '52494646'. What's the
best way to do that? I don't know what the former string will be, be I
know it will be exactly four characters.

I'm searching a binary file for the latter string, but I'm given the
former as the search term. I'm thinking something like this for the
search:

puts File.read('file.wav').index('52494646')

So... best way to do the conversion, and best way to do the search.
Ideas?

Earle
--
Posted via http://www.ruby-....

1 Answer

Gordon Thiesfeld

10/23/2007 3:22:00 PM

0

> So... best way to do the conversion, and best way to do the search.
> Ideas?
>
> Earle

Here's a way to do the conversion. No ideas on the search.

class String
def to_hex_string
r = ''
self.each_byte{|c| r << c.to_s(16)}
r
end
end

# unit test
require 'test/unit'

class TestString < Test::Unit::TestCase

def test_convert
assert_equal( '52494646', 'RIFF'.to_hex_string)
end

end

Regards,

Gordon