Python Unicode 对象和 C API(从 pyunicode 对象中检索 char*) [英] Python Unicode object and C API ( retrieving char* from pyunicode objects )

查看:36
本文介绍了Python Unicode 对象和 C API(从 pyunicode 对象中检索 char*)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在将我所有的 C++ 引擎类绑定到 python 以用于游戏脚本目的.最新的挑战是,当您在脚本中创建一个字符串时,例如

I am currently binding all of my C++ engine classes to python for game play scripting purposes. The latest challenge is that when say you make a variable in the script a string such as

string = 'hello world'

这变成了一个 PyUnicodeObject.接下来,我们要从绑定的 C 端函数调用脚本中此对象上的函数.PrintToLog( string ),作为一个例子,我们可以说这个 c 函数就是这样

this becomes a PyUnicodeObject. Next we want to call a function on this object in the script from a bound C side function. PrintToLog( string ), as an example lets say this c-function is as such

void PrintToLog( const char * thisString )
{
   //file IO stuff as expected
   myLog << thisString;
   //more stuff involving fileIO
}

所以我的绑定需要从 PyUnicodeObject 中提取一个 char * ,它首先会由 python 传递给我的通用函数处理程序,然后它将提取并转换 pyobjects 到正确的 c 端类型并将其传递给我的功能.

So my binding needs to extract a char * from the PyUnicodeObject which will be passed at first by the python to my generic function handler, which in turn will extract and covert the pyobjects to the proper c-side type and pass it to my function.

我能找到的唯一函数提取 wchar_t*... 无论如何都可以获得 ascii 表示,因为我们将只使用 ascii 字符集.

The only function I can find extracts a wchar_t*... Is there anyway to get the ascii representation since we will only be using the ascii character set.

我使用 Python 3.2,其中所有字符串都是 unicode

I am using Python 3.2 where all strings are unicode

推荐答案

我相信您正在寻找的功能是 PyUnicode_AsASCIIString.这会给你一个非 unicode (ASCII) python 字符串.然后您可以采用常规方法从中提取 char* .

I believe the function you're looking for is PyUnicode_AsASCIIString. This will give you a non-unicode (ASCII) python string. And then you can take the normal approach for extracting a char* from that.

这篇关于Python Unicode 对象和 C API(从 pyunicode 对象中检索 char*)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆