Paul Irofti
11/8/2007 2:51:00 PM
On 2007-11-08, Tom M <thomas.macklin@gmail.com> wrote:
> On Nov 8, 7:24 am, Jari Williamsson
><jari.williams...@mailbox.swipnet.se> 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