2

This is pretty simple to create. I have a simple byte array with proof that it has data in it on runtime : enter image description here

Then I simply do

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

It compiles, but I get an Argument Exception on runtime that says that the destination array is too small.

From microsoft msdn documentation :

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

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

ESD
  • 675
  • 2
  • 12
  • 35
  • 1
    Can you describe what problem you are trying to solve? Your destination type is 4 bytes long and you are trying to create such a value from only 3 bytes. The MSDN example uses 4 bytes. – Marcin Hoppe Mar 24 '15 at 13:57

1 Answers1

6

The size of integer (in C#) is 4 bytes. You need at least 4 bytes for the conversion to succeed. The sample shows that there are only 3.

(Not sure why the message says "destination array". It is rather "source".)

AlexD
  • 32,156
  • 3
  • 71
  • 65