PythonのC-APIとUnicode文字列
-
05-09-2019 - |
質問
私は、PythonオブジェクトとさまざまなエンコーディングのC文字列の間で変換する必要があります。 UnicodeオブジェクトにC文字列から行くには、PyUnicode_Decodeを使用してかなり簡単だった、しかしイム他の道を行く方法がわからない。
//char* can be a wchar_t or any other element size, just make sure it is correctly terminated for its encoding
Unicode(const char *str, size_t bytes, const char *encoding="utf-16", const char *errors="strict")
:Object(PyUnicode_Decode(str, bytes, encoding, errors))
{
//check for any python exceptions
ExceptionCheck();
}
私は例えば、PythonのUnicode文字列を受け取り、与えられたencodeingを使用してバッファにそれを置く別の関数を作成したい:
//fills buffer with a null terminated string in encoding
void AsCString(char *buffer, size_t bufferBytes,
const char *encoding="utf-16", const char *errors="strict")
{
...
}
私はそれは私が私のバッファにそれを置くするかどうかはわかりませんPyObjectを返ししかしPyUnicode_AsEncodedStringを行うには気にいらがあると疑われる...
注:両方の方法は、上記のPython APIをラップするC ++ Unicodeのクラスのメンバーであります 私は、Python 3.0を使用しています。
解決
私はそれは私が私のバッファにそれを置くするかどうかはわかりませんPyObjectを返ししかしPyUnicode_AsEncodedStringを行うには気にいらがあると疑われる...
PyObjectが返さPyStringObjectあるので、あなたは単なる文字列のバッファへのポインタを取得し、独自のバッファにそれをmemcpyをするPyString_Size
とPyString_AsString
を使用する必要があります。
、私はあなたがそれを行うことができないと思います。
所属していません StackOverflow