Large integers from C

John Machin sjmachin at lexicon.net
Wed Feb 20 18:19:57 EST 2002


Gustavo Niemeyer <niemeyer at conectiva.com> wrote in message news:<mailman.1014230314.2890.python-list at python.org>...
> [...]
> > I need to create Large (>300 bit) python Long's from C. I don't need (and
> > in fact, don't want) to use the features of the (apparently obselete?)
> > mpz module, or gmpy, or whatever. I simply want to say:
> > 
> > val = PyLong_FromOctets(char *v, int vlen);
> 
> You're probably looking for PyLong_FromString().

It is possible that the OP wants, but is not looking very hard for, this:

[from Python 2.2 source, in include/longobject.h]
"""
/* _PyLong_FromByteArray:  View the n unsigned bytes as a binary integer in
   base 256, and return a Python long with the same numeric value.
   If n is 0, the integer is 0.  Else:
   If little_endian is 1/true, bytes[n-1] is the MSB and bytes[0] the LSB;
   else (little_endian is 0/false) bytes[0] is the MSB and bytes[n-1] the
   LSB.
   If is_signed is 0/false, view the bytes as a non-negative integer.
   If is_signed is 1/true, view the bytes as a 2's-complement integer,
   non-negative if bit 0x80 of the MSB is clear, negative if set.
   Error returns:
   + Return NULL with the appropriate exception set if there's not
     enough memory to create the Python long.
*/
extern DL_IMPORT(PyObject *) _PyLong_FromByteArray(
        const unsigned char* bytes, size_t n,
        int little_endian, int is_signed);
"""



More information about the Python-list mailing list