From: Adam Shelly Date: 2008-01-31T09:29:48+09:00 Subject: Re: Newbie: working with binary files/extract png from a binary file On 1/30/08, Jim wrote: > Thank you both for your information and effort. > > Adam, your code worked out of the box. Tell me you didn't write that > off the top of your head? I patched it together based on wikipedia's PNG entry and parts of a WAV file reader I wrote earlier - they are both chunk-based file formats. It took a few failed tests before I got it right. > > m= /\211PNG/.match(fp.read) > # (fp.read) is a String of binary data. Using a regular ex. to locate the PNG header > fp.seek(m.begin(0)) > # I understand seek, Not sure what m.begin(0) is actually doing? > > Docs say: Returns the offset of the start of the nth element of the > match array in the string. > > I still don't get it. > from the same docs: "MatchData acts as an array. ... mtch[0] is equivalent to ... the entire matched string. " so m.begin(0) returns the offset of the start of the matched string (in this case, the start of the png header). Since the string contains the contents of the whole file, the string offset is the same as the file offset we need. -Adam