I have a simple code using ctypes:
Python 3.1.1 (r311:74480, Feb 23 2010, 11:06:41)
[GCC 4.1.2 20071124 (Red Hat 4.1.2-42)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
>>> libc = ctypes.CDLL("libc.so.6")
>>> libc.strlen("HELLO")
1
>>> print(libc.strlen("HELLO"))
1
What did I do wrong ?.
Thanks in advance.
Python 3 uses Unicode for strings. When passed to a C function those will be more than one byte per character, and for ASCII characters one of those bytes will be zero.
strlenwill stop at the first zero it finds.I’m able to duplicate these results in Python 2.7, along with a fix: