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

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Thu Sep 14 11:36:08 EDT 2006


In <1158247587.541483.266640 at k70g2000cwa.googlegroups.com>, John Machin
wrote:

> In other words, if the last byte of strg is NUL, throw it away.
> 
> The truly paranoid would code that as
>     if strg and strg[-1] etc etc
> so that it wouldn't blow up if strg is empty -- strange things can
> happen when you are reading other people's data :-)

I would spell it:

if strg.endswith('\0'):
    strg = strg[:-1]

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list