Error converting byte array to int array in C #

I am trying to convert a byte array to an int array declaration, and then convert a byte array to an int array.

To convert from a byte array to an int array, I used this code:

int[] iArray = new int[someSize]; byte[] bArray = new byte[iArray.Length * sizeof(int)]; Buffer.BlockCopy(iArray, 0,bArray, 0, bArray.Length); // This code works correctly. 

But when converting from a byte array to an int array, the values ​​in the iArray2 array become false when the value in the iArray array is iArray than 256 (maybe this is an overflow, I don't know.)

 // What is the error in this code?. int iArray2 = new int[someSize]; Buffer.BlockCopy(bArray, 0, iArray2, 0, iArray2.Length); 

How can I convert from byte array to int array correctly?

+6
source share
1 answer

Buffer.BlockCopy always deals with bytes, not array units.

Therefore, when you pass iArray2.Length in the second call to BlockCopy() , you copy as many bytes, which is one quarter of your actual array.

+7
source

Source: https://habr.com/ru/post/914263/


All Articles