Copying arrays in c# - c#

I am trying to copy an array in C#.
The definition of the first array is :
byte [][] a which contains 8 arrays each of size 8192 bytes.
The destination array is a 1d array of size 8192*8
Buffer.BlockCopy(a,0,b,0,8192*8)
where b is the destination array. However I keep getting Object must be an array of primitives. Does anyone know why this is happening?

That's because you don't have a single array of value, you have an array of references to arrays of values.
The byte[][] type is not the same as the byte[,] type. The first is an array of arrays (aka jagged array) while the second is a two dimensional array.
You would need to copy each array by itself:
for (int i = 0; i < a.Length; i++) {
Buffer.BlockCopy(a[i], 0, b, 8192 * i, 8192);
}

That is because your first input array is not an array of primitives. It is an array of byte[] arrays.
Buffer.BlockCopy(a(array of byte[]) , 0, b (array of byte),0,8192*8)
Fix:
Buffer.BlockCopy(a.SelectMany().ToArray(), 0, b, 0, 8192*8)
But to be honest I think you have the same result with:
var b = a.SelectMany().ToArray();

Related

Properly use a matrix double[,] in c#

Im trying to pass as parameter this matrix:
double[,] array2 = new double[4, 5]
{
{ 45, -6, 8, -3, 48 },
{ 0, -56.733333, 3.6444444, -6.8666667, 26.8666667 },
{ 0, 0, 78.17111712, -15.2432, -133.378378},
{ 0, 0, 0, 94.190193, 319.457667}
};
But when I try to use it in the other function i get problems with out of range, I dont know what I'm doing wrong since I'm using the same range that I specified when I created the objetc double[,] Im using the next code:
For passing the matrix:
Example1 a = new Example1();
a.function(array2,4,5);
Using the function:
public double[] function(double[,] nn, int n, int m)
{
double[,] ma = new double[n, m];
ma = nn;
double[] x = new double[5] { 0, 0, 0, 0, 0 };
x[n] = ma[n, m] / ma[n, n];
return x;
}
Im getting the error when I try to use ma[n,m] / ma[n,n] And I dont know why is happenig cause ma[n,m] it happens to exist and ma[n,n] happens to exist to.
If in doubt, read the documentation
Arrays (C# Programming Guide)
Array Overview
An array has the following properties:
An array can be Single-Dimensional, Multidimensional or Jagged.
The number of dimensions and the length of each dimension are established when the array instance is created. These values can't be
changed during the lifetime of the instance.
The default values of numeric array elements are set to zero, and reference elements are set to null.
A jagged array is an array of arrays, and therefore its elements are reference types and are initialized to null.
Arrays are zero indexed: an array with n elements is indexed from 0 to n-1.
Array elements can be of any type, including an array type.
Array types are reference types derived from the abstract base type Array. Since this type implements IEnumerable and IEnumerable<T>, you
can use foreach iteration on all arrays in C#.
Some further reading
Multidimensional Arrays
Zero-based numbering
Why are zero-based arrays the norm?
The problem is that the matrix does not recognize double [4,5] cause when declarated goes from [0..3] taking 4 spaces and the [4,#] will take 5 spaces.

How to remove bytes from a specific index from a byte array

I have a byte array . I need to remove the bytes at specific index and split the byte arrays. For example Say I have a byte array of length 1000 . And I need to remove the bytes from position 50 to 200 .So my expected result would be 2 byte arrays . One is 0-49 and another is 201-1000.
Is Array.RemoveAt the only way to remove the byte array with index?
Thanks in advance!
If speed is critical, you can create two new arrays and use Array.Copy() to copy the required bytes into them.
To make this easier to use, it might be more convenient to write a little extension method for arrays which extracts and returns a subset, like this (note: error handling omitted for brevity):
public static class ArrayExt
{
public static T[] Subset<T>(this T[] array, int start, int count)
{
T[] result = new T[count];
Array.Copy(array, start, result, 0, count);
return result;
}
}
Then you can use it like so (I have corrected the index from your example; you had it starting at 201, but it should have been 200):
var array1 = new byte[1000];
// ... populate array1 somehow, then extract subsets like so:
var array2 = array1.Subset( 0, 50);
var array3 = array1.Subset(200, 800);
// Now array2 and array3 are two byte arrays
// containing the required bytes.
This is probably the fastest you are likely to get.
You could use IEnumerable Take and Skip, for example
byte[] origin = new byte[200];
for(int i = 0; i < origin.Length; i++)
origin[i] = (byte)i;
byte[] first = origin.Take(50).ToArray();
byte[] second = origin.Skip(100).Take(50).ToArray();
Below code can be used to split the byte array, "index" starting index and "count" is the index count upto which index you want to split, here for your condition index=50 and count is 150;
List<byte> byteArray = array.ToList(); //array is the byte array
byteArray.RemoveRange(index, count);
Byte[] array1 = byteArray.ToArray(); //array1 is the resultant byte array
Cant you get the sub arrays like this?
byte[] array1 = array.ToList().GetRange(0, x1-1).ToArray();
byte[] array2 = array.ToList().GetRange(x2, array.Length- x2 - 1).ToArray();
where x1 and x2 is 50 and 200 in your example

Error in converting byte array to int array in C#

I am trying to convert a byte array to an int array ad then convert the byte array back to an int array.
For converting from byte array to 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 the byte array to in int array, the values in the iArray2 array becomes false when the value in the iArray array is larger than 256 (may be it is overflow, I do not 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?
Buffer.BlockCopy always deals in bytes, not array units.
Therefore, when you pass iArray2.Length in the second BlockCopy() call, you're copying that many bytes, which is one quarter of your actual array.

get every nth byte/value in array

I'm looking for a smooth/fast way to retrieve every nth short in a byte array and copy it to a new array.
l = lowerbyte
u = upperbyte
My data is of the following form:
byte[] bytes= {a0l, a0u, b0l, b0u, c0l, ..., n0l, n0l, a1l, a1u, b1l, ..., nXl, nXu}
What I need is to get get n byte arrays of length X (e.g., a[0..X], b[0..X], ... or M[a..n][0..X])
I was thinking of the following two steps:
convert values to short (=> short[] shorts= { a0, b0, c0, ... n0, a1, .. nX})
by using something like
short[] shorts= new short[(int)(Math.Ceiling(bytes.Length / 2.0)];
Buffer.BlockCopy(bytes, 0, shorts, 0, bytes.Length);
retrieve every second value from shorts
I'm looking for some fast code here... something like blockcopy with skip
I am completely aware that I could use a loop - but maybe there's a better
solution to it as I need to do this task for 80MB/s...
convert it back to byte arrays (same same - using blockcopy)
byte[] arrayX = new byte[shorts.Length * 2];
Buffer.BlockCopy(shorts, 0, arrayX , 0, arrayX .Length);
Thank you so much!
I think you might as just well copy the bytes directly to the new byte array, having calculated the correct offset for each short.
Converting it all to a separate short array and back to a byte array is a waste of time IMO.

Add a dimension to a multidimensional array in c#

I have a multidimensional array
byte[,] matrix;
and i want copy in a 3 dimension array
byte[,,] 3dplan;
in this way
3dplan[,,0]=matrix
What is the fastest way to accomplish this task in c#?
You need to manually copy the elements in a nested loop; there is no faster way.
If you switch to a jagged array (byte[,][] or byte[][][]), you can insert the smaller array as-is into a slot in the larger array (although they will both refer to the same array instance and will pick up changes)
You can copy 2d array into 3d array with Buffer.BlockCopy:
var _3dplan = new int[2, 2, 2];
var _2dplan = new int[2, 2] { { 1, 1 }, { 1, 1 } };
var index = 0;
var size = _2dplan.Length * sizeof(int);
Buffer.BlockCopy(_2dplan, 0, _3dplan, index * size, size);

Categories