转换字节数组INT [英] Converting BYTE array to INT

查看:278
本文介绍了转换字节数组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是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?

If all you have is two bytes, maybe you meant to use ToInt16?

这篇关于转换字节数组INT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆