How to search for ascii char sequence in binary file

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs