1

I'm having problems converting a wstring to jstring in unix, as the size of wchar_t on linux in 4 bytes (not 2 bytes like windows and thus I cannot use the casting of a wchar_t to a jchar).

Can anyone please help me with that?

Thanks, Reza

1 Answer 1

2

You have to use something like iconv(), because C++ wide strings have an opaque (read: unknown) encoding, while Java expects UTF16. Try this:

#include <iconv.h> #include <string> #include <vector> #include <iostream> std::u16string convert(std::wstring s) { iconv_t cd = iconv_open("UTF-16BE", "WCHAR_T"); if (cd == iconv_t(-1)) { std::cout << "Error while initializing iconv: " << errno << std::endl; iconv_close(cd); return std::u16string(); } std::size_t n = s.length() * 2 + 1; // Each character might use up to two CUs. const std::size_t norig = n; std::size_t m = s.length() * sizeof(std::wstring::value_type); std::vector<char16_t> obuf(n); char * outbuf = reinterpret_cast<char*>(obuf.data()); const char * inbuf = reinterpret_cast<const char*>(&s[0]); const std::size_t ir = iconv(cd, const_cast<char**>(&inbuf), &m, &outbuf, &n); if (ir == std::size_t(-1)) { std::cout << "Error while converting with iconv(): " << errno << ":" << EINVAL << ", left " << m << ", written " << std::dec << norig - n << " bytes." << std::endl; iconv_close(cd); return std::u16string(); } iconv_close(cd); return std::u16string(obuf.data(), (norig - n)/sizeof(std::u16string::value_type)); } 

If you don't have char16_t and std::u16string, you can use uint16_t as the basic character type and std::basic_string<uint16_t> or std::vector<uint16_t> as the resulting container.

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

5 Comments

Thanks. So this method is converting a wstring to a utf16 string, right? Then, how should I create a jstring out of this wstring?
@RezaPlusPlus: The input is an opaque C++ wide string, and the output of this function is a well-defined UTF-16BE string. I don't know what jstrings are, but you ought to be able to pass a pointer to the first element of the result to the jstring in one way or another.
BTW, is std::u16string standard STL?
@RezaPlusPlus: There's no such thing as "standard STL"; "STL" is a historical artefact from 1994 :-) u16string is part of the C++11 standard. If you don't have support for that, you can use a vector of uint16_ts instead, as I suggest in the final paragraph.
Thanks and sorry that I missed the last paragraph.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.