gpt4 book ai didi

python - 将 Python 长整数转换为 C 字符数组

转载 作者:太空宇宙 更新时间:2023-11-04 02:13:51 24 4
gpt4 key购买 nike

我正在使用 Python/C API,并希望 C 函数采用 Python 长整型参数 nPyLong,并将其转换为字符数组 nString ,以 10 为基数。这是我想做的事情的片段。

PyObject *nPyLong;
PyArg_ParseTuple(args, "O", nPyLong);

PyLong_Check(nPyLong) // returns true

const char *nString;
// This function doesn't exist but demonstrates the functionality
PyLong_AsCharArray(&nString, nPyLong, 10);

API 引用中提到了一个函数 PyObject* PyLong_FromString(char *str, char **pend, int base)将 C char 数组转换为 Python 长整型,给定任意基数,但我找不到执行相反操作的函数。

这可能吗?一种想法是将 Python 长整数转换为 Python 字符串对象,然后再转换为 C 字符数组,但也没有处理长整数的字符串函数。似乎 C 处理 Python 长整型的唯一方法是转换为 C 整型,所有这些都会在我的应用程序中溢出。

最佳答案

使用PyObject_Str() (或 PyObject_Bytes())并将结果对象转换为 char*

关于python - 将 Python 长整数转换为 C 字符数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10263871/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com