【发布时间】:2026-02-13 04:30:01
【问题描述】:
什么是 C# byte[] 的 C++(和/或 Visual-C++)类比?
【问题讨论】:
标签: c# c++ visual-c++ bytearray byte
什么是 C# byte[] 的 C++(和/或 Visual-C++)类比?
【问题讨论】:
标签: c# c++ visual-c++ bytearray byte
byte[] 在 C# 中是一个无符号 8 位整数数组 (byte)。
等价物是uint8_t array[]。
uint8_t 在 stdint.h (C) 和 cstdint (C++) 中定义,如果您的系统上没有提供它们,您可以轻松下载它们,或者自己定义它们(参见this SO question)。
【讨论】:
unsigned char
char 不保证是 8 位类型。
uint8_t,定义如下:typedef unsigned char uint8_t,所以它是以字符为模型的。如果 char 大于 8 位,uint8_t 也会如此。还是我错了?我之前所说的只是uint8_t 只是表明您的意图,即该变量应仅保存范围 [0 255] 内的值,但在某些平台上,没有什么能阻止您(除了糟糕的编程习惯)将更大的数字放入其中。
C++ 中最接近的等效类型是动态创建的“unsigned char”数组(除非您在将字节定义为 8 位以外的处理器上运行)。
例如
在 C# 中
byte[] array = new byte[10];
在 C++ 中
unsigned char *array = new unsigned char[10];
【讨论】:
byte[] array = new byte[10]吗?
在 C++ 标准中,char、signed char 和 unsigned char 是三种可用的字符类型。 char 可能是 signed 或 unsigned,因此:
typedef signed char sbyte;
typedef unsigned char byte;
byte bytes[] = { 0, 244, 129 };
【讨论】: