Created
October 22, 2013 15:54
-
-
Save eberle1080/7103201 to your computer and use it in GitHub Desktop.
An example of how to use PyUnicode_AsASCIIString
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Assumption: you have a variable named "pyobj" which is | |
// a pointer to an instance of PyUnicodeObject. | |
PyObject* temp = PyUnicode_AsASCIIString(pyobj); | |
if (NULL == temp) { | |
// Means the string can't be converted to ASCII, the codec failed | |
printf("Oh noes\n"); | |
return; | |
} | |
// Get the actual bytes as a C string | |
char* c_str = PyByteArray_AsString(temp); | |
// Use the string in some manner | |
printf("The python unicode string is: %s\n", c_str); | |
// Make sure the temp stuff gets cleaned up at the end | |
Py_XDECREF(temp); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment