【问题标题】:Convert unsigned char* to const char* - Android [closed]将 unsigned char* 转换为 const char* - Android [关闭]
【发布时间】:2016-02-25 18:08:45
【问题描述】:

我正在尝试将 unsigned char* 转换为一个 const char* 并且我知道存在 reinterpret_cast<const char*> 但我不能在 上使用它>android,总是打印垃圾。

您能建议我另一种方法吗?

【问题讨论】:

  • 你确定 reinterpret_cast 导致垃圾,而不是它已经是垃圾?
  • 你有什么样的数据以及如何“打印”。 const char* 可能被 I/O 视为 C 样式字符串。
  • 我会复制它,因为来源是const
  • 实际上想做什么?您能尝试创建一个Minimal, Complete, and Verifiable Example 并展示给我们看吗?还有read about the XY problem.
  • @JamesRoot 你是对的,唯一我不怀疑的事情,似乎我现在开始编码了。

标签: android c++ android-ndk char


【解决方案1】:

您可以直接将其键入(以 C 风格)转换为所需的类型。

例如

unsigned char* ptr_unsigned = new unsigned char[10];

const char* ptr_const = (const char*) ptr_unsigned;

希望这会有所帮助。

【讨论】:

  • C 风格的转换只是不清楚的函数风格的转换。我认为无论如何这都会调用重新解释演员表。
猜你喜欢
  • 1970-01-01
  • 2011-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多