From: daz Date: 2004-04-27T06:54:03+09:00 Subject: Re: String#unpack and null-terminated strings Nobu wrote: > daz wrote in [ruby-talk:98364]: > > Combining that with recognition of the length specifier: > > I'd also considered about it, but > > > > > s = "abc\0def\0\0jkl\0" > > > > s.unpack('Z6Z*Z*') #-> ["abc", "f", ""] > > It can't round trip with Array#pack, so I discarded this plan. > But the user has specified that the first field is fixed-width(6) and null-terminated so: "abc\000de" == "abc\000\000\000" ==> "abc" Everything from "\000" to the end of the field is junk because the user told us so by using 'Z'. We don't need to apologise that pack didn't replace the exact junk that was there before :-? Round trip: s = "abc\000def\000\000jkl\000" zf = 'Z6Z*Z*' s.unpack(zf) #-> ["abc", "f", ""] s.unpack(zf).pack(zf) #-> "abc\000\000\000f\000\000" s.unpack(zf).pack(zf).unpack(zf) #-> ["abc", "f", ""] The fixed width consumes the added zero padding bytes so it doesn't create bogus extra fields. --- To me, the result below seems _not_ to do what was requested: s.unpack('Z6Z*Z*') #-> ["abc\000de", "f", ""] I'm probably missing a crucial point here? daz