Python オブジェクトとさまざまなエンコーディングの C 文字列との間で変換する必要があります。ac 文字列から Unicode オブジェクトへの移行は、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 文字列を受け取り、指定されたエンコードを使用してバッファに入れる別の関数を作成したいと思います。
//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")
{
...
}
PyUnicode_AsEncodedString と関係があると思われますが、それは PyObject を返すので、それをバッファに入れる方法がわかりません...
注: 上記の両方のメソッドは、Python 3.0 を使用している Python API をラップする C++ Unicode クラスのメンバーです。