How do I converted a null (0) terminated string to a Python string?
Robert Kern
robert.kern at gmail.com
Thu Sep 14 12:20:37 EDT 2006
Robert Kern wrote:
> Michael wrote:
>> I guess, I still don't see how this will work. I'm receiving a C
>> zero-terminated string in my Python program as a 1K byte block (UDP
>> datagram). If the string sent was "abc", then what I receive in Python
>> is <a><b><c><0><garbage><garbage>...<last_garbage_byte>. How is Python
>> going to know where in this 1K byte block the end of the string is? It
>> seems that what I need to do is tell Python that the string ends at
>> zero-relative index 3. What am I missing here?
>
> Nothing. This is what I would do:
>
>
> In [34]: s
> Out[34]: 'abc\x00garbage'
>
> In [35]: s.split('\x00', 1)[0]
> Out[35]: 'abc'
And I see that this is the advice that John Machin already gave you. Shame on me
for not reading the thread before replying.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
More information about the Python-list
mailing list