How to search for ascii char sequence in binary file

E

Earle Clubb

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
 
G

Gordon Thiesfeld

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top