Search code examples
c#bytebitconverter

BitConverter Exception, the destination array is too small


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


Solution

  • 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".)