How to convert a C string (char array) into a Python string when there are non-ASCII characters in the string?

后端 未结 3 1256
星月不相逢
星月不相逢 2020-12-19 06:06

I have embedded a Python interpreter in a C program. Suppose the C program reads some bytes from a file into a char array and learns (somehow) that the bytes represent text

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-19 06:52

    You don't want to decode the string into a Unicode representation, you just want to treat it as an array of bytes, right?

    Just use PyString_FromString:

    char *cstring;
    PyObject *pystring = PyString_FromString(cstring);
    

    That's all. Now you have a Python str() object. See docs here: https://docs.python.org/2/c-api/string.html

    I'm a little bit confused about how to specify "str" or "unicode." They are quite different if you have non-ASCII characters. If you want to decode a C string and you know exactly what character set it's in, then yes, PyString_DecodeString is a good place to start.

提交回复
热议问题