How to convert uint64 in C into Python 32bit Object [ I am usingPython2.2 ]

  • Thread starter Explore_Imagination
  • Start date
E

Explore_Imagination

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.

Looking forward for your positive feedback.

Cheers
 
J

John Machin

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
 
E

Explore_Imagination

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 ?
 
S

Saju Pillai

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top