On Dec 11, 6:45 pm, Explore_Imagination <[EMAIL PROTECTED]> wrote: > On Dec 11, 4:45 am, John Machin <[EMAIL PROTECTED]> wrote: > > > > > On Dec 11, 9:49 am, Explore_Imagination <[EMAIL PROTECTED]> > > wrote: > > > > Hi all > > > > I am new to C and python ... I want to convert C data type uint64 > > > variable into the Python 32bit Object. I am currently using Python 2.2 > > > [ It is necessary to use it ] > > > > Kindly give your suggestion how and in which way I can achieve this > > > task. > > > I'm not sure what you mean by "the Python 32bit Object". A Python int > > object holds a signed 32-bit integer. A Python long object holds a > > signed integer of arbitrary size. You will need to convert your uint64 > > into a Python long; then, if necessary, check that the result will fit > > in an int (result <= sys.maxint). > > > If the "C variable" is in an 8-byte string that you have read from a > > file, the unpack function in the struct module will do the job. > > Assuming your computer is little-endian: > > > >>> maxu64 = '\xff' * 8 # example input string > > >>> import struct > > >>> result = struct.unpack('<Q', maxu64)[0] > > >>> result > > > 18446744073709551615L>>> 2 ** 64 - 1 > > > 18446744073709551615L > > > If however you mean that in C code you need to build a Python object > > to pass over to Python code: According to the Python/C API Reference > > Manual (http://www.python.org/doc/2.2.3/api/longObjects.html): > > > PyObject* PyLong_FromUnsignedLongLong(unsigned long long v) > > Return value: New reference. > > Returns a new PyLongObject object from a C unsigned long long, or > > NULL on failure. > > > If however you mean something else, .... > > > HTH, > > John > > Thanks for your feedback ... Actually I want to pass unit64 variable > in C to python > but at the same time I want to have a generic code which should work > on both little-endian > and big endian architectures > > Any suggestions ?
I am not sure if endianness comes into the picture. As long as sizeof (uint64) == sizeof(unsigned long long) on your platform, python should have no problem accepting the return value of PyLong_FromUnsignedLongLong(uint64_var); srp -- http://saju.net.in -- http://mail.python.org/mailman/listinfo/python-list