문제

파이썬 객체와 다양한 인코딩의 C 문자열을 변환해야합니다. AC 문자열에서 유니 코드 객체로가는 것은 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 String을 가져 와서 주어진 인코딩을 사용하여 버퍼에 넣는 다른 함수를 만들고 싶습니다.

//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를 반환하여 버퍼에 넣는 방법을 잘 모르겠습니다 ...

참고 : 위의 두 가지 방법 모두 C ++ 유니 코드 클래스의 구성원입니다. Python 3.0을 사용하고 있습니다.

도움이 되었습니까?

해결책

나는 그것이 pyunicode_asencodedstring과 관련이 있다고 생각하지만 pyobject를 반환하여 버퍼에 넣는 방법을 잘 모르겠습니다 ...

반환 된 pyobject는 pystringobject이므로 사용하면됩니다. PyString_Size 그리고 PyString_AsString 문자열의 버퍼에 대한 포인터를 가져 와서 자신의 버퍼에 memcpy를 사용하십시오.

갈 길을 찾고 있다면 곧장 Pyunicode 객체에서 자신의 Char 버퍼로, 나는 당신이 그렇게 할 수 있다고 생각하지 않습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top