Skip to main content
 首页 » 编程设计

c#之BitConverter 异常,目标数组太小

2024年02月06日13over140

创建起来非常简单。我有一个简单的字节数组,证明它在运行时有数据:

那我就干

var bytedata = BitConverter.ToUInt32(byte_array,0); 

它可以编译,但我在运行时收到一个 Argument Exception,指出目标数组太小。

来自微软 msdn 文档:

byte[] bytes = { 0, 0, 0, 25 }; 
int i = BitConverter.ToInt32(bytes, 0); 

https://msdn.microsoft.com/en-us/library/bb384066.aspx

请您参考如下方法:

整数(在 C# 中)的大小是 4 个字节。您至少需要 4 个字节才能使转换成功。示例显示只有 3 个。

(不确定消息为什么说“目标数组”。它更像是“源”。)