I'm having trouble while attempting to extract a row from my array. This is my code :
using System;
namespace C_sharp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!. This is the one !");
dynamic[] res = new Object[3];
res[0] = new int[2,2]{{3,2},{16,9}};
res[1] = new int[2,2]{{4,7},{6,29}};
res[2] = new int[2,2]{{30,29},{5,49}};
//dynamic resultat = res[0][0];
}
}
}
As you can see, at the end of my program, the contents of my local variable res looks like this :
[0] [dynamic]:{int[2, 2]}
[0, 0] [int]:3
[0, 1] [int]:2
[1, 0] [int]:16
[1, 1] [int]:9
[1] [dynamic]:{int[2, 2]}
[0, 0] [int]:4
[0, 1] [int]:7
[1, 0] [int]:6
[1, 1] [int]:29
[2] [dynamic]:{int[2, 2]}
[0, 0] [int]:30
[0, 1] [int]:29
[1, 0] [int]:5
[1, 1] [int]:49
Now, I want to put only the first row of res[0] in my variable resultat in order to get a 1D array. So, at the end of the process, resultat should be equal to :
[0] [int]:3
[1] [int]:2
I've already tried res[0][0,] and res[0][0:]. None of them worked. Any help ?
You can flatten 2d array using .Cast<int>() and then take first row using .Take(2):
const int arraySize = 2;
var resultat = (res[0] as int[,]).Cast<int>().Take(arraySize).ToArray();
In general case for an arbitrary row index you can use .Skip(arraySize * rowIndex)
If it is too slow for you, you may try Buffer.BlockCopy:
const int rowSize = 2;
const int intSize = 4;
int[] resultat = new int[rowSize];
Buffer.BlockCopy(res[0], 0, resultat, 0, intSize * rowSize);
In general case it would be
const int intSize = 4; // Size of integer type in bytes
int[,] matrix = res[0];
int rowSize = matrix.GetLength(1); // Get size of second 2d-array dimension
int rowIndex = 0; // Index of a row you want to extract
int[] resultat = new int[rowSize];
Buffer.BlockCopy(res[0], // Copy source
rowSize * intSize * rowIndex, // Source offset in bytes
resultat, // Copy destination
0, // Destination offset
intSize * rowSize); // The number of bytes to copy
Buffer.BlockCopy documentation
Related
I want to do something like this
int amountOfDimensions = 3;
Array a = new Array(3);
but of course that is not a thing in C#,
but is there's something that do the same thing ?
Try Array.CreateInstance, e.g.
// array of int with 3 dimensions with correspondent lengths 2, 3 and 4:
// i.e. result will be int[2, 3, 4]
var result = Array.CreateInstance(typeof(int), 2, 3, 4);
I have a 2d array of UInt16s which I've converted to raw bytes - I would like to take those bytes and convert them back into the original 2D array. I've managed to do this with a 2d array of doubles, but I can't figure out how to do it with UInt16.
Here's my code:
UInt16[,] dataArray;
//This array is populated with this data:
[4 6 2]
[0 2 0]
[1 3 4]
long byteCountUInt16Array = dataArray.GetLength(0) * dataArray.GetLength(1) * sizeof(UInt16);
var bufferUInt16 = new byte[byteCountUInt16Array];
Buffer.BlockCopy(newUint16Array, 0, bufferUInt16, 0, bufferUInt16.Length);
//Here is where I try to convert the values and print them out to see if the values are still the same:
UInt16[] originalUInt16Values = new UInt16[bufferUInt16.Length / 8];
for (int i = 0; i < 5; i++)
{
originalUInt16Values[i] = BitConverter.ToUInt16(bufferUInt16, i * 8);
Console.WriteLine("Values: " + originalUInt16Values[i]);
}
The print statement does not show the same values as the original 2d array. I'm pretty new to coding with bytes and UInt16 so most of this I'm learning in the process.
*Also, I know the last chunk of my code isn't putting values into a 2d array like the original array - right now I'm just trying to print out the values to see if they even match the original data.
If what you want is just to cast UInt16[,]->Byte, and then Byte->UInt16 you can do another Block copy, which is very fast at run-time, code should look like this:
UInt16[,] dataArray = new UInt16[,] {
{4, 6, 2},
{0, 2, 0},
{1, 3, 4}
};
for (int j = 0; j < 3; j++)
{
for (int i = 0; i < 3; i++)
{
Console.WriteLine("Value[" + i + ", " + j + "] = " + dataArray[j,i]);
}
}
long byteCountUInt16Array = dataArray.GetLength(0) * dataArray.GetLength(1) * sizeof(UInt16);
var bufferUInt16 = new byte[byteCountUInt16Array];
Buffer.BlockCopy(dataArray, 0, bufferUInt16, 0, bufferUInt16.Length);
//Here is where I try to convert the values and print them out to see if the values are still the same:
UInt16[] originalUInt16Values = new UInt16[bufferUInt16.Length / 2];
Buffer.BlockCopy(bufferUInt16, 0, originalUInt16Values, 0, BufferUInt16.Length);
for (int i = 0; i < 5; i++)
{
//originalUInt16Values[i] = BitConverter.ToUInt16(bufferUInt16, i * 8);
Console.WriteLine("Values---: " + originalUInt16Values[i]);
}
by the way, you only divided each UInt16 into two bytes, so you should calculate your new size dividing by two, not eight
The program
public static void Main(string[] args)
{
UInt16[,] dataArray = new ushort[,]{ {4,6,2}, {0,2,0}, {1,3,4}};
//This array is populated with this data:
long byteCountUInt16Array = dataArray.GetLength(0) * dataArray.GetLength(1) * sizeof(UInt16);
var byteBuffer = new byte[byteCountUInt16Array];
Buffer.BlockCopy(dataArray, 0, byteBuffer, 0, byteBuffer.Length);
for(int i=0; i < byteBuffer.Length; i++) {
Console.WriteLine("byteBuf[{0}]= {1}", i, byteBuffer[i]);
}
Console.WriteLine("Byte buffer len: {0} data array len: {1}", byteBuffer.Length, dataArray.GetLength(0)* dataArray.GetLength(1));
UInt16[] originalUInt16Values = new UInt16[byteBuffer.Length / 2];
for (int i = 0; i < byteBuffer.Length; i+=2)
{
ushort _a = (ushort)( (byteBuffer[i]) | (byteBuffer[i+1]) << 8);
originalUInt16Values[i/2] = _a;
Console.WriteLine("Values: " + originalUInt16Values[i/2]);
}
}
Outputs
byteBuf[0]= 4
byteBuf[1]= 0
byteBuf[2]= 6
byteBuf[3]= 0
byteBuf[4]= 2
byteBuf[5]= 0
byteBuf[6]= 0
byteBuf[7]= 0
byteBuf[8]= 2
byteBuf[9]= 0
byteBuf[10]= 0
byteBuf[11]= 0
byteBuf[12]= 1
byteBuf[13]= 0
byteBuf[14]= 3
byteBuf[15]= 0
byteBuf[16]= 4
byteBuf[17]= 0
Byte buffer len: 18 data array len: 9
Values: 4
Values: 6
Values: 2
Values: 0
Values: 2
Values: 0
Values: 1
Values: 3
Values: 4
You see that a ushort, aka UInt16 is stored in a byte-order in which 4 = 0x04 0x00, which is why I chose the conversion formula
ushort _a = (ushort)( (byteBuffer[i]) | (byteBuffer[i+1]) << 8);
Which will grab the byte at index i and take the next byte at i+1 and left shift it by the size of a byte (8 bits) to make up the 16 bits of a ushort. In orhter words, ushort _a = 0x[second byte] 0x[first byte], which is then repeated. This conversion code is specific for the endianess of the machine you are on and thus non-portable.
Also I fixed the error where the byteBuffer array was to big because it was multiplied with factor 8. A ushort is double the size of a byte, thus we only need factor 2 in the array length.
Addressing the title of your question (Convert byte[] to UInt16):
UInt16 result = (UInt16)BitConverter.ToInt16(yourByteArray, startIndex = 0);
Your casting up so you should be able to do things implicitly
var list = new List<byte> { 1, 2 ,
var uintList = new List<UInt16>();
//Cast in your select
uintList = list.Select(x => (UInt16)x).ToList();
I've taken a 2D array of UInt16 values, and converted it to raw bytes. I would like to take those bytes and convert them back into the original 2D array, but I'm unsure of how to do this when I only have the bytes, i.e., is there a way to determine the dimensions of an original array when all you have is that array converted to bytes?
Here's my code:
UInt16[,] dataArray = new UInt16[,] {
{4, 6, 2},
{0, 2, 0},
{1, 3, 4}
};
long byteCountUInt16Array = dataArray.GetLength(0) * dataArray.GetLength(1) * sizeof(UInt16);
var bufferUInt16 = new byte[byteCountUInt16Array];
Buffer.BlockCopy(dataArray, 0, bufferUInt16, 0, bufferUInt16.Length);
//Here is where I try to convert the values and print them out to see if the values are still the same:
UInt16[] originalUInt16Values = new UInt16[bufferUInt16.Length / 2];
Buffer.BlockCopy(bufferUInt16, 0, originalUInt16Values, 0, BufferUInt16.Length);
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Values---: " + originalUInt16Values[i]);
}
This code will put the bytes into a 1-dimensional array, but I would like to put them into the original 2d array. Is this possible when if all I have are the raw bytes? I'll eventually be sending these bytes via a REST call and the receiving side will only have the bytes to convert back into the original 2D array.
So... not certain exactly what you're specifications are, but you could send the dimensions (x,y) of the array as the first four bytes of your buffer. below is my crack at it. I heavily commented it so hopefully it should make sense there. Ask any questions if that code isn't clear.
/**** SENDER *****/
// ushort and UInt16 are the same (16-bit, 2 bytes)
ushort[,] dataArray = new ushort[,] {
{4, 6, 2},
{0, 2, 0},
{1, 3, 4}
};
// get the X and Y dimensions
ushort xDim = (ushort)dataArray.GetLength(0);
ushort yDim = (ushort)dataArray.GetLength(1);
// Make an array for the entire 2D array and the dimension sizes
ushort[] toSend = new ushort[xDim * yDim + 2];
// load the dimensions into first two spots in the array
toSend[0] = xDim;
toSend[1] = yDim;
// load everything else into the array
int pos = 2;
for (int i = 0; i < xDim; i++)
{
for (int j = 0; j < yDim; j++)
{
toSend[pos] = dataArray[i, j];
pos += 1;
}
}
// size of the array in bytes
long byteCountUInt16Array = sizeof(ushort) * (xDim * yDim + 2);
// create the byte buffer
var bufferUInt16 = new byte[byteCountUInt16Array];
// copy everything (including dimensions) into the byte beffer
Buffer.BlockCopy(toSend, 0, bufferUInt16, 0, bufferUInt16.Length);
/***********RECEIVER************/
// get the dimensions from the received bytes
ushort[] xyDim = new ushort[2];
Buffer.BlockCopy(bufferUInt16, 0, xyDim, 0, sizeof(ushort) * 2);
// create buffer to read the bytes as ushorts into, size it based off of
// dimensions received.
ushort[] readIn = new ushort[xyDim[0] * xyDim[1]];
Buffer.BlockCopy(bufferUInt16, sizeof(ushort) * 2, readIn, 0, sizeof(ushort) * readIn.Length);
// create 2D array to load everything into, size based off of received sizes
ushort[,] originalUInt16Values = new ushort[xyDim[0], xyDim[1]];
// load everything in
int cur = 0;
for (int i = 0; i < xyDim[0]; i++)
{
for (int j = 0; j < xyDim[1]; j++)
{
originalUInt16Values[i, j] = readIn[cur];
cur += 1;
}
}
// print everything out to prove it works
for (int i = 0; i < xyDim[0]; i++)
{
for (int j = 0; j < xyDim[1]; j++)
{
Console.WriteLine("Values at {0},{1}: {2}", i, j, originalUInt16Values[i, j]);
}
}
// uhh... keep the console open
Console.ReadKey();
You can't get the original dimensions. Example:
8 bytes = [0, 1, 0, 2, 0, 1, 0, 2]
into array of 16 bits (2 bytes):
= [1, 2, 1, 2]
into array of 64 bits (4 bytes):
= [65538, 65538]
and all of these ways (1 byte, 2 bytes, 4 bytes) are valid for parsing, so you must indicate your original sizes, or at least one of them. Luckily you may send the sizes (or sizes) in the headers of the request. This may do the trick for what you want.
Another way of doing this is what serial systems do: simply concat your size (or sizes) and your buffer.
size [4 bytes = Int32] + buffer [n bytes]
finally parse the first bytes to read the size and block copy starting from 1 first byte of your buffer (don't forget the offset. In the example above you should start block copying from byte number 5)
I am new to Unity3D and c#. I am tinkering with storing some grid positions within a 2d array however I've run into
the array index is out of range
error and I don't know why:
public int[,] myArray;
myArray = new int[,]{
{0,375},
{75,300},
{150,225},
{225,150},
{300,75},
{375,0}
};
Debug.Log(myArray[1,4]); // array index is out of range... why? I expected to get 75.
Here are some other resources I was looking at for help:
http://wiki.unity3d.com/index.php/Choosing_the_right_collection_type
https://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx
You have a 2D array which is 6x2 - not one which is 2x6. Each "subarray" you're specifying in the initialization is one "row", if you think of accessing the array by array[row, column].
So for example, myArray[0, 1] is 375 - the second element of the first "row", which is { 0, 375 }.
Basically, you need to pivot either your array initialization, or your array access. So if you really want a 2x6 array, you'd need:
myArray = new int[,] {
{ 0, 75, 150, 225, 300, 375 },
{ 375, 300, 225, 150, 75, 0 }
};
... or you could keep the existing initialization, and access myArray[4, 1].
The C# specification explains it like this:
For a multi-dimensional array, the array initializer must have as many levels of nesting as there are dimensions in the array. The outermost nesting level corresponds to the leftmost dimension and the innermost nesting level corresponds to the rightmost dimension. The length of each dimension of the array is determined by the number of elements at the corresponding nesting level in the array initializer. For each nested array initializer, the number of elements must be the same as the other array initializers at the same level. The example:
int[,] b = {{0, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 9}};
creates a two-dimensional array with a length of five for the leftmost dimension and a length of two for the rightmost dimension:
int[,] b = new int[5, 2];
and then initializes the array instance with the following values:
b[0, 0] = 0; b[0, 1] = 1;
b[1, 0] = 2; b[1, 1] = 3;
b[2, 0] = 4; b[2, 1] = 5;
b[3, 0] = 6; b[3, 1] = 7;
b[4, 0] = 8; b[4, 1] = 9;
I'm working on a project that requires me to pass 2D arrays between C# and Unmanaged C++.
I need to do this for 2D integer array sand 2D float arrays but right now I'm stuck on the 2D integer arrays.
I have a pattern for 1D integer arrays working just fine....
My demo C# code looks like this
// Test 2D integer Array
int[,] cs2DIntArray = new int[5,2];
cs2DIntArray[0, 0] = 0;
cs2DIntArray[0, 1] = 1;
cs2DIntArray[1, 0] = 10;
cs2DIntArray[1, 1] = 11;
cs2DIntArray[2, 0] = 20;
cs2DIntArray[2, 1] = 21;
cs2DIntArray[3, 0] = 30;
cs2DIntArray[3, 1] = 31;
cs2DIntArray[4, 0] = 40;
cs2DIntArray[4, 1] = 41;
int my2DArrayIntReturn = tess.test2DIntArray(cs2DIntArray, 5, 2);
"tess" is a managed C++ Wrapper class and implements the test2DIntArray method like this,
int test2DIntArray(array<int, 2>^ my2DIntArray, int rows, int columns){
using System::Runtime::InteropServices::GCHandle;
using System::Runtime::InteropServices::GCHandleType;
GCHandle my2DIntArrayGCHandle = GCHandle::Alloc(my2DIntArray,GCHandleType::Pinned);
IntPtr my2DIntArrayPtr = my2DIntArrayGCHandle.AddrOfPinnedObject();
my2DIntArray[0, 0] = 0;
my2DIntArray[0, 1] = 11;
my2DIntArray[1, 0] = 110;
my2DIntArray[1, 1] = 111;
my2DIntArray[2, 0] = 120;
my2DIntArray[2, 1] = 121;
my2DIntArray[3, 0] = 130;
my2DIntArray[3, 1] = 131;
my2DIntArray[4, 0] = 140;
my2DIntArray[4, 1] = 141;
return pu->uTest2DIntArray((int*)my2DIntArrayPtr.ToPointer(),rows,columns);
};
pu is the unmanaged class and implements uTest2DIntArray like this
int UnmanagedModel::uTest2DIntArray(int* my2DIntArray, int arrayRows, int arrayColumns)
{
my2DIntArray[0,0] = 20;
my2DIntArray[0,1] = 21;
my2DIntArray[1,0] = 210;
my2DIntArray[1,1] = 211;
my2DIntArray[2,0] = 220;
my2DIntArray[2,1] = 221;
my2DIntArray[3,0] = 230;
my2DIntArray[3,1] = 231;
my2DIntArray[4,0] = 240;
my2DIntArray[4,1] = 241;
return my2DIntArray[1,1];
}
This compiles and runs without errors or warnings BUT when the code returns to C# only the first two values [0,0] and [0,1] reflect the changes made to the values in the unmanaged code as shown below. All of the other values reflect the changes made in the managed C++ method.
cs2DIntArray {int[5, 2]} int[,]
[0, 0] 240 int
[0, 1] 241 int
[1, 0] 110 int
[1, 1] 111 int
[2, 0] 120 int
[2, 1] 121 int
[3, 0] 130 int
[3, 1] 131 int
[4, 0] 140 int
[4, 1] 141 int
Can anyone see/explain what I'm doing wrong and what needs to be done to correct this code? I haven't coded in a while and I did spend quite a while looking for solutions on Google but can't quite get this work as desired.
Any guidance will be gratefully received.
Doug
Further information
If I change the indexing in the unmanaged code to use a single index (from 0 to 9) instead of a 2D index (0,0 to 4,1) then all of the values are updated correctly
In the debugger, when I look at the address of each array value[x,y] - they all show exactly the same address
Using 2D array indexing it seems like the first index is being completely ignored - the only the second index is being used and since this varies from 0 to 1 the code is simply overwriting the first two array values.
Which tells me I do have access to the correct memory for the array inside my unmanaged code, but I can't use 2D indexing to get to it. Which is a pain, and has to fixable.
c++ doesn't have 2-dimensional arrays. The comma-operator turns 4,0 into 0, so my2DIntArray[4,0] is equivalent to my2DIntArray[0].
Since my2DIntArray is a pointer to a one-dimensional array, you have to simulate 2 dimensions by manually calculating the index like this: my2DIntArray[line * columns + column]