From: Rafael Date: 2008-07-03T05:47:03+09:00 Subject: Re: Problem with unpack On 2 Jul., 12:35, Peña, Botp wrote: > From: Rafael [mailto:mayoral.raf...@gmail.com] > # strhex = str.unpack("H*") > > you assume the return value is a string :) > > botp@botp-desktop:~$ qri string.unpack > ---------------------------------------------------------- String#unpack >      str.unpack(format)   => anArray > ------------------------------------------------------------------------ >      Decodes str (which may contain binary data) according to the >      format string, returning an array of each value extracted. The >      format string consists of a sequence .... > > verify it by using #class method > eg, > >   p strhex.class > > # and look for the desired group within this "transformed" string > # if (strhex.include?("02001000")) == true then > > c/c++-ism ;) lose the "== true" portion, > >   if strhex[0].include?("02001000") > > or much better, > >   strhex = str.unpack("H*")[0]  #<---note the index for 1st elem >   if strhex.include?("02001000") >      puts "The tag was found" >   .... > > or something like that > > # As I said, this doesn't work (it reports the tag was not found!) and I > # wonder whether it has something to do with the fact that > # > # strhex.length # -> 1 > > you got a hint there.  should have check it further ;) > > # By the way, if after unpacking I do > # puts strhex > > try, > >   p strhex > > # then I get the correct string, that is exactly the same as hexdump > # shows me on the left-hand side. > # So strhex seems to contain what I need, but still there is some kind > # of problem. > > that implies that you are very close to your objective ... > > hth. > > kind regards -botp Thank you, this hint did it! I had read the documentation about unpack and also the bit about the return value being an array. But then the table with the different unpack directives had confused me. The column "Returns" says "String" at the "H" directive. Now I understand, the return type of "unpack" is array and if one uses the directive "H", then the elements of that array are of type string. Got it! And yes, I must recognize I am C++ biased. But I'm learning. Thanks Cheers Rafael