在python3中将PyBytesObject类型转换为PyUnicodeObject类型
如何将 pyunicodeobject 类型转换为 pybytesobject 类型?
示例:
function(PyBytesObject* byteobj){
....operation..
}
PyUnicodeObject* Uniobj;
function((PyBytesObject*) Uniobj);
结果出现总线错误。
How to convert pyunicodeobject type to pybytesobject type?
Example:
function(PyBytesObject* byteobj){
....operation..
}
PyUnicodeObject* Uniobj;
function((PyBytesObject*) Uniobj);
got a bus error as a result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要对其进行编码,就像在 Python 中进行编码一样。对于 utf-8 使用:
或者,如果您想要 utf-16 或其他编码格式,也有适用于这些的 api。请参阅文档 http://docs.python.org/py3k/c- api/unicode.html(搜索以
PyUnicode_As
开头的函数)。进行编码时不要忘记检查返回码,并在完成编码后释放对 bytes 对象的引用。
You need to encode it just as you would if you were doing it in Python. For utf-8 use:
Or if you want it in utf-16 or some other encoding there are api's for those too. See the docs at http://docs.python.org/py3k/c-api/unicode.html (search for functions beginning with
PyUnicode_As
).Don't forget to check the return code when you do the encoding, and release the reference to the bytes object when you're done with it.