【问题标题】:How to Declare byte* ( byte array ) in c++?如何在 C++ 中声明 byte*(字节数组)?
【发布时间】:2013-04-11 08:57:54
【问题描述】:

如何在c++中声明byte*(字节数组)以及如何在函数定义中定义为参数?

当我像下面这样声明时

函数声明:

int Analysis(byte* InputImage,int nHeight,int nWidth);

得到错误:“字节”未定义

【问题讨论】:

  • 使用 unsigned char 代替。一样的......一个字节
  • 那么我应该如何将 byte* 转换为 unsigned char,从 C# 应用程序获取字节数组输入。
  • 您想在 c++ 中将字节从 c# 转换为 char 吗?我理解正确吗?
  • 我认为您可以通过在传递给 c++ char 之前将 byte 转换为 c# 中的 char 来做到这一点
  • 这个应该可以帮助你将c#中的字节转换为char stackoverflow.com/questions/5431004/…

标签: c++ bytearray


【解决方案1】:

表示一个字节的 C++ 类型是 unsigned char(或 char 的其他符号风格,但如果你想要它作为纯字节,unsigned 可能就是你所追求的)。

但是,在现代 C++ 中,您不应该使用原始数组。如果您的数组是运行时大小的,请使用std::vector<unsigned char>,或者如果您的数组是静态大小的N,请使用std::array<unsigned char, N> (C++11)。您可以通过 (const) 引用将这些传递给函数,如下所示:

int Analysis(std::vector<unsigned char> &InputImage, int nHeight, int nWidth);

如果Analysis 不修改数组或其元素,则改为:

int Analysis(const std::vector<unsigned char> &InputImage, int nHeight, int nWidth);

【讨论】:

    【解决方案2】:

    C++ 中没有 byte 类型。您应该使用 typedef 之前。类似的东西

    typedef std::uint8_t byte;
    

    在 C++11 中,或

    typedef unsigned char byte;
    

    在 C++03 中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-23
      • 2014-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多