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 %s or %r format specifier (you'll decimal representation)
  • you can't use %x format specifier (you'd have convert pyobject* c int first, 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

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -