When the character string passed to C ++ by JNA is garbled

Case study

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.

Character code problem

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.

solution

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);
}

Conclusion

I'm glad I learned about character codes.

References

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

When the character string passed to C ++ by JNA is garbled
I want to find out if the specified character string is supported by the target character code
[Swift] Copy the character string to the clipboard
When the hover of Eclipse is hard to see
[Java] When putting a character string in the case of a switch statement, it is necessary to make it a constant expression
[Java] How to cut out a character string character by character
[Kotlin] Convert ZonedDateTime to String by specifying the format
# 1_JAVA I want to get the index number by specifying one character in the character string.
Use JLine when you want to handle keystrokes on the console character by character in Java
% rails db: When creating, the LoadError caused by mimemagic is
Change only one character from the String type string to uppercase
A program that searches for a character string, and when the search character string is found, displays the character string from the beginning of the line to just before the search character string.
When SimpleDateFormat is garbled like ٢٠١٨١٠٠٤٠٨٣١٣٣٦٥٧
Monkey patch to return the current time when an empty string is specified in Ruby's Time.parse
[Java beginner] Conversion from character string to numerical value-What is the parseInt method of the Integer class? ~
When requested access to the resource is denied when pushing with Docker
Countermeasures for forgetting to specify the font when titleTextAttributes is specified
How to output the value when there is an array in the array
When the processing after conditional branching is redundant-12 [C # refactoring sample]
[Rails] How to omit the display of the character string of the link_to method
How to write the view when Vue is introduced in Rails?
Convert iso-2022-jp character string to utf-8
[IOS] What to do when the image is filled with one color
What to do if the prefix c is not bound in JSP
Invoke the character string passed as an argument as a method with send
[Docker] How to build when the source code is bind-mounted on the container
[Java small story] Monitor when a value is added to the List
I want to find out what character the character string appears from the left
[Java] Check if the character string is composed only of blanks (= Blank)
[Ruby] How to prevent errors when nil is included in the operation
<Java> Quiz to batch convert file names separated by a specific character string with a part of the file name