c++ - 在 C++ 中将 unsigned char (byte) 数组转换为 unsigned short

标签 c++ arrays unsigned unsigned-char

我有 array^ byteArray 并且我需要提取 Little Endian 序列中的字节以生成无符号短整型和整数。我已经尝试了以下我能想到的所有组合,所以我正在寻求帮助。

int x = UInt32(byteArray[i]) + (UInt32)(0x00ff) * UInt32(byteArray[i + 1]);
int x = UInt32(byteArray[i]) + UInt32(0x00ff) * UInt32(byteArray[i + 1]);
int x = byteArray[i] + 0x00ff * byteArray[i + 1];

问题是最低有效字节(在 i+1)我知道它是 0x50 但生成的 short/int 报告低字节为 0x0b。高字节不受影响。

我认为这是一个符号错误,但我似乎无法修复它。

最佳答案

您正在使用托管代码。 Endian-ness 是框架知道的实现细节:

array<Byte>^ arr = gcnew array<Byte> { 1, 2, 3, 4 };
int value = BitConverter::ToInt16(arr, 1);
System::Diagnostics::Debug::Assert(value == 0x302);

框架的假设是否正确取决于数据的来源。

关于c++ - 在 C++ 中将 unsigned char (byte) 数组转换为 unsigned short,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7542391/

相关文章:

php - 如何在php中将对象数组转换为键值对

java - 在java中处理无符号数据的公认方法是什么

c++ - wchar_t 只是 unsigned short 的 typedef 吗?

c++ - 将 char* 转换为 uint8_t

c++ - 从现有对象创建对象

c++ - 如何使用 FFMPEG 从存储为视频 AVPackets 的 NAL 单元播放 H.264 流

c++ - 在 C++ 中是否有 "generic"迭代器类型用于函数参数?

c++ - QT如何使用setPixmap skin.copy()做动画?

android - 我可以通过数组定义多个 LinearLayouts 吗?

javascript - 在JS数组中设置对象参数的问题