【问题标题】:C++ equivalent to C# Encoding.ASCII.GetBytes()C++ 等价于 C# Encoding.ASCII.GetBytes()
【发布时间】:2017-10-26 07:59:28
【问题描述】:

我来自 C#,我有一条线:

 byte[] key = Encoding.ASCII.GetBytes("myString");

如何在 C++ 中使用等价物来获得 std::vector<unsigned char>

【问题讨论】:

  • 你真的需要它吗?你想用std::vector<unsigned char> 实现什么是你无法使用std::basic_string<unsigned char> 实现的?
  • 您想从一种字符集转换为另一种吗?在这种情况下,请查看std::codecvt。如果你只需要unsigned charbyte:它们是一样的,不需要转换。

标签: c++ encoding ascii


【解决方案1】:

这样可以解决问题吗?

unsigned char buffer[mystring.length()];
memcpy(buffer, mystring.data(), mystring.length());

【讨论】:

  • 确实,我以为会更复杂
  • 这要复杂得多,因为您的 C# 代码要做的事情要多得多。如果此 C++ 代码对您有好处,那么您的 .NET 字符串始终只包含 C0 Controls and Basic Latin,或者您的 C# 代码是错误的。
猜你喜欢
  • 2023-03-08
  • 2021-07-20
  • 1970-01-01
  • 1970-01-01
  • 2020-04-23
  • 1970-01-01
  • 2011-09-19
  • 2014-09-20
  • 1970-01-01
相关资源
最近更新 更多