【问题标题】:Is there C# `Encoding.UTF8.GetString` equivalent in C++?C++ 中是否有等效的 C#`Encoding.UTF8.GetString`?
【发布时间】:2016-09-13 12:35:41
【问题描述】:

C++ 中是否有 C# Encoding.UTF8.GetString 等价物?或者另一种快速的方法来解析包含字节序列的字节数组并解码为字符串。

【问题讨论】:

  • 是很好的解决方案,但我无法通过index 解析字节数组,例如Encoding.UTF8.GetString(byte[], index int, count int);
  • 答案很大程度上取决于“字符串”的含义。最接近 C# 的String 可能是std::wstring,在这种情况下,您可能正在寻找std::wstring_convert<codecvt_utf8<wchar_t>>().from_bytes(your_utf8_byte_ptr)

标签: c# c++ visual-c++


【解决方案1】:

你可以试试这个:

auto *wstrBytes = new wchar_t[size];
memcpy_s(wstrBytes , size, rawBytes, size);
std::wstring unicodeStr(wstrBytes , size);
delete [] wstrBytes;

【讨论】:

    猜你喜欢
    • 2019-06-23
    • 2012-04-02
    • 1970-01-01
    • 2014-05-05
    • 2018-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-30
    相关资源
    最近更新 更多