Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to search for ascii char sequence in binary file

Reply
Thread Tools

How to search for ascii char sequence in binary file

 
 
Earle Clubb
Guest
Posts: n/a
 
      10-23-2007
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/.

 
Reply With Quote
 
 
 
 
Gordon Thiesfeld
Guest
Posts: n/a
 
      10-23-2007
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
reading binary file into memory. Converting from char to uint32,float, double, ASCII strings etc (static_cast< > ?) someone C++ 37 10-18-2011 01:32 AM
Binary tree search vs Binary search Bogdan C Programming 22 10-21-2010 09:46 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM



Advertisments