How to search for ascii char sequence in binary file

Discussion in 'Ruby' started by Earle Clubb, Oct 23, 2007.

  1. Earle Clubb

    Earle Clubb Guest

    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-forum.com/.
    Earle Clubb, Oct 23, 2007
    #1
    1. Advertising

  2. > 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
    Gordon Thiesfeld, Oct 23, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marc Schellens
    Replies:
    8
    Views:
    2,978
    John Harrison
    Jul 15, 2003
  2. Karl Heinz Buchegger

    char, unsigned char, and binary file io

    Karl Heinz Buchegger, Apr 22, 2004, in forum: C++
    Replies:
    1
    Views:
    572
    glen_stark
    Apr 22, 2004
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,005
    Ian Collins
    May 9, 2006
  4. Bogdan

    Binary tree search vs Binary search

    Bogdan, Oct 18, 2010, in forum: C Programming
    Replies:
    22
    Views:
    3,027
    Michael Angelo Ravera
    Oct 21, 2010
  5. someone
    Replies:
    37
    Views:
    2,464
    Joshua Maurice
    Oct 18, 2011
Loading...

Share This Page