转换字节数组INT [英] Converting BYTE array to INT
问题描述
我有这种code的
static void Main(string[] args)
{
byte[] array = new byte[2] { 0x00, 0x1f };
Console.WriteLine(BitConverter.ToInt32(array, 0));
}
但它不工作。它抛出一个异常:
However it does not work. It throws an exception:
目的地阵列不够长以复制在所有项目
采集。检查数组索引和长度。
Destination array is not long enough to copy all the items in the collection. Check array index and length.
什么是错的?
推荐答案
正如其名,一个<一个href=\"http://msdn.microsoft.com/en-us/library/system.int32%28v=vs.110%29.aspx\"><$c$c>Int32$c$c>是32位或4个字节,所以如果你想为一个字节数组转换为的Int32
,它需要有至少4的长度,(或更多precisely,它需要有至少开始位置之后4个字节)。
As the name suggests, an Int32
is 32 bits, or 4 bytes, so if you want to convert a byte array to an Int32
, it needs to have a length of at least 4, (or at more precisely, it needs to have least 4 bytes after the start position).
如果你拥有的是两个字节,也许你想用<一个href=\"http://msdn.microsoft.com/en-us/library/system.bitconverter.toint16%28v=vs.110%29.aspx\"><$c$c>ToInt16$c$c>?
If all you have is two bytes, maybe you meant to use ToInt16
?
这篇关于转换字节数组INT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!