1

I'm trying to build a Python extension with C++ 14. When the module is initialized in Python, I ask caller giving me a callback function which take a string as input.

When a function in the module is called, I want to do is reading a text file from disk, which may contain non-English characters. So I read it into memory as wstring in C++ code.

The next step is to pass this wstring to Python code. Should I define the user callback function like below:

typedef std::string (*UserCallbackFunc)(std::string);

and handle wstring to utf-8 string conversion like below:

string wstring2utf8string(wstring input)
{
    wstring_convert<codecvt_utf8<wchar_t>> converter;
    return converter.to_bytes(input);
}

or I could simply define the callback like

typedef std::wstring (*UserCallbackFunc)(std::wstring);

and pass in what I read from disk directly?

1 Answer 1

1

Let me answer the question by myself.

My goal is to pass non-English characters from C++ made Python 3 extension to Python callback function. Apparently UTF-8 is good enough for me to achieve this goal other than using UTF-16. In C++, string is good enough for UTF-8 and wstring is for UTF-16 and UTF-32. So after I change all wstring to string, the non-English character in UTF-8 working fine from both sides.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.