How do I converted a null (0) terminated string to a Python string?

G

George Sakkis

Michael said:
Robert,

Thanks to you and everyone else for the help. The "s.split('\x00',
1)[0] " solved the problem.

And a probably faster version: s[:s.index('\x00')]

George
 
R

Robert Kern

George said:
Michael said:
Robert,

Thanks to you and everyone else for the help. The "s.split('\x00',
1)[0] " solved the problem.

And a probably faster version: s[:s.index('\x00')]

Yup. About twice as fast for at least one dataset:


In [182]: import timeit

In [183]: t1 = timeit.Timer("s.split('\\x00', 1)[0]", "s='abc\\x00'*256")

In [184]: t2 = timeit.Timer("s[:s.index('\\x00')]", "s='abc\\x00'*256")

In [192]: t1.repeat(3, 100000)
Out[192]: [0.68063879013061523, 0.67146611213684082, 0.66347002983093262]

In [193]: t2.repeat(3, 100000)
Out[193]: [0.35819387435913086, 0.35968899726867676, 0.37595295906066895]

--
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top