Calling the 'hex' builtin from the python C API -
i building c extension module python 3.x. access functionality of hex builtin in python layer. is, convert (form c code) pyobject* of type 'pylong_type' (that is, plain python int) pyobject* of type pyunicode_type , represents hexadecimal coding of int started with.
this seems should easy, none of functions in integer section of api guide seem it; neither of functions in string section. note in particular pyunicode_fromformat doesn't need:
- you can't use
%sor%rformat specifier (you'll decimal representation) - you can't use
%xformat specifier (you'd have convertpyobject*cintfirst, isn't safe thing do, since python integer might large fit.
this implementation of hex:
static pyobject * builtin_hex(pyobject *self, pyobject *v) { return pynumber_tobase(v, 16); } it's static, implementation suggests easy way same functionality:
return pynumber_tobase(your_number, 16); pynumber_tobase exists in 2.6 , 2.7, though hex doesn't use in 2.x line.
Comments
Post a Comment