When passing Java String type to C ++ with JNA, if it is received as Char * type and assigned to std: string type, it will be garbled.
It seems that ** UTF-16 ** is used in Java, and it seems that ** Shift-JIS ** was required in my C ++ environment this time.
In C ++, it seems that the character code that can be handled by each type changes depending on the environment. Thank you @yumetodo.
I'm guessing that the conversion wasn't working and I got an error.
I have quoted a part of the code here. http://nekko1119.hatenablog.com/entry/2017/01/02/054629
use.cpp
#include <iostream>
#include <string>
#include <codecvt>
#include <cassert>
#include <locale>
void useString(char* str){
std:string strUtf;
strUtf = str;
/* uft-Shift from 8-Convert to JIS*/
strShiftJis = utf8_to_multi_cppapi(strUtf);
/*Please like from here on*/
}
std::string utf8_to_multi_cppapi(std::string const& src)
{
auto const wide = utf8_to_wide_cppapi(src);
return wide_to_multi_capi(wide);
}
std::string wide_to_multi_capi(std::wstring const& src)
{
std::size_t converted{};
std::vector<char> dest(src.size() * sizeof(wchar_t) + 1, '\0');
if (::_wcstombs_s_l(&converted, dest.data(), dest.size(), src.data(), _TRUNCATE, ::_create_locale(LC_ALL, "jpn")) != 0) {
throw std::system_error{ errno, std::system_category() };
}
return std::string(dest.begin(), dest.end());
}
std::wstring utf8_to_wide_cppapi(std::string const& src)
{
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
return converter.from_bytes(src);
}
I'm glad I learned about character codes.
http://marupeke296.com/CPP_charUnicodeWideChar.html http://nekko1119.hatenablog.com/entry/2017/01/02/054629 https://cpprefjp.github.io/reference/codecvt/codecvt_utf8_utf16.html https://theolizer.com/cpp-school1/cpp-school1-18/
Recommended Posts