【转载】unicode编码转换

 

 Py_Initialize();
 PyObject * pModule = NULL;
 PyObject * pFunc = NULL;
 PyObject * pArg = NULL;
 PyObject * pRet = NULL;
 PyObject * Uni  = NULL;
 char *p = NULL;

 pModule = PyImport_ImportModule("Python");
 pFunc   = PyObject_GetAttrString(pModule, "Pythonit");
 
#if 1
 std::string p_obj = "你好";
 int wlen=::MultiByteToWideChar(CP_ACP,NULL,p_obj.c_str(),int(p_obj.size()),NULL,0);
 wchar_t *wszString = new wchar_t[wlen+1];
 ::MultiByteToWideChar(CP_ACP,NULL,p_obj.c_str(),int(p_obj.size()),wszString,wlen);
 wszString[wlen]='\0';
 PyObject* pb=PyUnicode_FromUnicode((const Py_UNICODE*)wszString,wlen);
 delete wszString;

#endif
 pArg = PyTuple_New(1);
 PyTuple_SetItem(pArg,0,pb);
 //PyTuple_SetItem(pArg,0,Py_BuildValue("s", "123"));
 //pArg    = Py_BuildValue("s", "123");
 pRet    = PyEval_CallObject(pFunc, pArg);
 PyArg_Parse(pRet,"s",&p);


 Py_Finalize();

你可能感兴趣的:(【转载】unicode编码转换)