How to convert C++ char*(contain \0) to python string in PyPy?

We used ffi.string() to convert C++ char* to pypy string. But recently, we found out ffi.string() used '\0' as the indicator of string termination. And it bring some problems of converting a binary string. Our case is we read char*(buf) from a binary file, and buf might contain '\0'. Using ffi.string() we were not able to convert completed buf to PyPy string. We've tried ffi.string(buf, length), but it also did not work. Are there any solutions?

Use ffi.buffer and not ffi.string On Thu, Nov 19, 2015 at 4:24 AM, Yicong Huang <hengha.mao@gmail.com> wrote:
We used ffi.string() to convert C++ char* to pypy string. But recently, we found out ffi.string() used '\0' as the indicator of string termination. And it bring some problems of converting a binary string.
Our case is we read char*(buf) from a binary file, and buf might contain '\0'. Using ffi.string() we were not able to convert completed buf to PyPy string. We've tried ffi.string(buf, length), but it also did not work. Are there any solutions?
_______________________________________________ pypy-dev mailing list pypy-dev@python.org https://mail.python.org/mailman/listinfo/pypy-dev

Great thanks Alex and Maciej. ffi.buffer works as expected. On Thu, Nov 19, 2015 at 2:04 PM, Maciej Fijalkowski <fijall@gmail.com> wrote:
Use ffi.buffer and not ffi.string
On Thu, Nov 19, 2015 at 4:24 AM, Yicong Huang <hengha.mao@gmail.com> wrote:
We used ffi.string() to convert C++ char* to pypy string. But recently, we found out ffi.string() used '\0' as the indicator of string termination. And it bring some problems of converting a binary string.
Our case is we read char*(buf) from a binary file, and buf might contain '\0'. Using ffi.string() we were not able to convert completed buf to PyPy string. We've tried ffi.string(buf, length), but it also did not work. Are there any solutions?
_______________________________________________ pypy-dev mailing list pypy-dev@python.org https://mail.python.org/mailman/listinfo/pypy-dev
participants (2)
-
Maciej Fijalkowski
-
Yicong Huang