You can create a simple C strtod
wrapper:
#include <stdlib.h>
double strtod_wrap(const char *nptr, char **endptr)
{
return strtod(nptr, endptr);
}
compile with:
gcc -fPIC -shared -o libstrtod.dll strtod.c
(if you're using Python 64 bit, the compiler must be 64-bit as well)
and call it using ctypes
from python (linux: change .dll
to .so
in the lib target and in the code below, this was tested on Windows):
import ctypes
_strtod = ctypes.CDLL('libstrtod.dll')
_strtod.strtod_wrap.argtypes = (ctypes.c_char_p, ctypes.POINTER(ctypes.c_char_p))
_strtod.strtod_wrap.restype = ctypes.c_double
def strtod(s):
p = ctypes.c_char_p(0)
s = ctypes.create_string_buffer(s.encode('utf-8'))
result = _strtod.strtod_wrap(s, ctypes.byref(p))
return result,ctypes.string_at(p)
print(strtod("12.5hello"))
prints:
(12.5, b'hello')
(It's not as hard as it seems, since I learned how to do that just 10 minutes ago)
Useful Q&As about ctypes