Dave Challis
Dave Challis

Reputation: 3906

Getting python ctypes datatype from numpy datatype

In python, is there a convenient way of getting a ctypes.c_* datatype that corresponds to a numpy datatype?

E.g.

numpy.float32 -> ctypes.c_float
numpy.float64 -> ctypes.c_double
numpy.uint16 -> ctypes.c_uint16

etc.

Upvotes: 2

Views: 1592

Answers (1)

simonzack
simonzack

Reputation: 20938

np.ctypeslib.as_ctypes seems to work:

>>> np.ctypeslib.as_ctypes(np.float32())
c_float(0.0)
>>> np.ctypeslib.as_ctypes(np.float64())
c_double(0.0)
>>> np.ctypeslib.as_ctypes(np.uint16())
c_ushort(0)

Upvotes: 7

Related Questions