0

In structure, a member was declared as 2-Dimensional array which name as myData and layout to 2*3.

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct myStruct
{
    [MarshalAs(UnmanageType.ByValArray, SizeConst = 2 * 3)]
    public byte[,] myData;
}

I declare a myStruct variable and name as testStruct. After the data was written to myData. I can't to read the data from myData.

case1. string myDataString = Encoding.UTF8.GetString(testStruct.myData[0]);

ErrorMsg: The number of index in [] incorrect, must be 2.

case2. string myDataString = Encoding.UTF8.GetString(testStruct.myData[0,0]);

ErrorMsg: Can not form 'byte' convert to 'byte[]'

case3.

byte temp = new byte[3];
temp = testStruct.myData[0];

ErrorMsg: The number of index in [] incorrect, must be 2.

case4. string myDataString = testStruct.myData[0].ToString;

ErrorMsg: The number of index in [] incorrect, must be 2.

case5. string myDataString = testStruct.myData[0,0].ToString;

ErrorMsg: Can not convert method ToString convert to string.

All of above cases I tried were show errors. How can I extract the data and save as byte[] or string ??

Is there has wrong with declare myData Array ??

I expect the data can be read 1-Dimensional per times. It is convenient to parsing the data from Multi-Dimensional Array by loop.

Yves
  • 1
  • 1
  • Does this answer your question? https://stackoverflow.com/q/27427527/5133585 – Sweeper May 12 '23 at 05:27
  • Does this answer your question? [Span and two dimensional Arrays](https://stackoverflow.com/questions/52750582/span-and-two-dimensional-arrays) – Charlieface May 12 '23 at 11:29

1 Answers1

0

I'm going to assume that the actual goal is serialization/deserialization of the array. Most tools cannot handle 2D arrays, so you need to convert it to a 1D array. You can do this by copying the data to a new 1D array:

var source = new byte[3, 2];
var target = new byte[6];
Buffer.BlockCopy(source, 0, target, 0, 6);

However, it may be a better alternative to use a 1D array in the first place, and write your own indexer to treat it as 2D data.

public struct MyMatrix
{
    public int Width { get; }
    public int Height { get; }
    public byte[] Data = new byte[6];

    public MyMatrix(int width, int height)
    {
        Width = width;
        Height = height;
        Data = new byte[width * height];
    }

    public byte this[int x, int y] { 
        get => Data[y * Width + x];
        set => Data[y * Width + x] = value;
    }
}

Note that the example lacks range checks for each index, so myMatrix[5, 0] would not throw an exception.

See How to base64 encode/decode if you want to convert the array to a string.

JonasH
  • 28,608
  • 2
  • 10
  • 23
  • I try the first part, it was not work. Is the 2-Dim Array initial wrong? Is it need to another process? Just like new(). I am not sure the correct way about the declare a 2-Dim array in a structure. Maybe it is wrong way for declare 2-Dim array in my example code. Would you demonstrate the correct way.? Thank you! – Yves May 12 '23 at 07:25
  • @Yves If you want to assign an array to a field in a structure you just do `myStruct.myData = new byte[2,3]` or `new myStruct(){myData = new byte[2,3]}`, But assignments/construction is a fairly fundamental thing to ask about, Maybe take a look in the programming guide? – JonasH May 12 '23 at 07:43
  • Thanks your reply. All you mentioned, I already tried before ask the question. It is different result, for declare 2-D array in a structure and declare 2-D array in a general function. In general case, it is simple to initial and read/write data for single-dim array or multi-dim array. But in case of declare multi-dim array at structure, it seems not initial finished when access this array. – Yves May 12 '23 at 08:45
  • See also https://stackoverflow.com/a/73374582/14868997 and https://stackoverflow.com/questions/52750582/span-and-two-dimensional-arrays/73373888#73373888 – Charlieface May 12 '23 at 11:29