gpt4 book ai didi

c# - 将 BYTE 数组转换为 INT

转载 作者:可可西里 更新时间:2023-11-01 08:04:05 24 4
gpt4 key购买 nike

我有这种代码

static void Main(string[] args)
{
byte[] array = new byte[2] { 0x00, 0x1f };
Console.WriteLine(BitConverter.ToInt32(array, 0));
}

但是它不起作用。它抛出异常:

Destination array is not long enough to copy all the items in the collection. Check array index and length.

怎么了?

最佳答案

顾名思义,一个 Int32是 32 位,即 4 个字节,所以如果要将字节数组转换为 Int32,它的长度至少需要 4,(或者更准确地说,它至少需要 4起始位置之后的字节)。

如果你只有两个字节,也许你打算使用 ToInt16

关于c# - 将 BYTE 数组转换为 INT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21639586/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com