From: Paul Irofti Date: 2007-11-08T23:55:01+09:00 Subject: Re: Buffer to string On 2007-11-08, Tom M wrote: > On Nov 8, 7:24 am, Jari Williamsson > wrote: >> I have a buffer (stored in a String class) with a NULL-terminated >> C-style string inside, what's the most efficient approach to get that >> text string up to the NULL? >> >> Best regards, >> >> Jari Williamsson > > Would buf.unpack("a*") work? Depends on the raw data contained within the C zero terminated string. If its plain text "a*" wouldn't work, but "Z*" might. For unpack I'd suggest seeing String#unpack for other formating and the way to combine the different format markers in order to retrieve the wanted data. $ irb irb(main):001:0> s = "Hello\0" => "Hello\000" irb(main):002:0> s.unpack("a*") => ["Hello\000"] irb(main):003:0> s.unpack("Z*") => ["Hello"] -- everything is simple, we're stupid contact at gmail