简体   繁体   中英

Convert Python long integer to C char array

I am using the Python/C API and would like a C function to take a Python long integer argument, nPyLong , and convert it to a char array, nString , in base 10. Here is a snippet of what I would like to do.

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);

The API reference mentions a function PyObject* PyLong_FromString(char *str, char **pend, int base) which converts a C char array to a Python long integer, given an arbitrary base, but I cannot find a function which does the inverse.

Is this possible? One idea is to convert the Python long integer to a Python string object and then convert to a C char array, but there are no string functions to handle long ints either. It seems the only way for C to handle Python long ints is to convert to C int types, all of which would overflow in my application.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM