在 Linux 上将 wstring 转换为 jstring

发布于 2024-12-18 02:24:03 字数 136 浏览 2 评论 0原文

我在unix中将wstring转换为jstring时遇到问题,因为linux上wchar_t的大小为4字节(而不是像Windows那样的2字节,因此我无法使用wchar_t到jchar的转换)。

有人可以帮我吗?

谢谢, 礼萨

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情徒 2024-12-25 02:24:03

您必须使用像 iconv() 这样的东西,因为 C++ 宽字符串具有不透明(读取:未知)编码,而 Java 需要 UTF16。试试这个:

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

如果您没有 char16_tstd::u16string,您可以使用 uint16_t 作为基本字符类型,并使用 std::basic_stringstd::vector 作为结果容器。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文