将byte[]转换为原始2d数组

本文关键字:原始 2d 数组 转换 byte | 更新日期: 2023-09-27 18:08:40

我已经采取了UInt16值的2D数组,并将其转换为原始字节。我想把这些字节转换回原来的二维数组,但我不确定如何做到这一点,当我只有字节,也就是说,有没有一种方法来确定原始数组的尺寸,当你所有的数组转换成字节?

下面是我的代码:

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]);
}

这段代码将把字节放入一维数组中,但我想把它们放入原始的二维数组中。如果我所有的都是原始字节,这可能吗?我最终将通过REST调用发送这些字节,接收端将只有字节转换回原始的2D数组。

将byte[]转换为原始2d数组

所以…不确定您的具体规格是什么,但是您可以将数组的尺寸(x,y)作为缓冲区的前四个字节发送。下面是我的尝试。我对此做了大量评论,希望大家能理解。如果代码不清楚,请提出任何问题。

  /**** 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();

无法获得原始尺寸。例子:

8 bytes = [0,1,0,2,0,1,0,2]

转换成16位(2字节)数组:= [1,2,1,2]

进入64位(4字节)数组:= [65538, 65538]

和所有这些方式(1字节、2字节、4字节)对于解析都是有效的,因此您必须指出您的原始大小,或者至少其中一种。幸运的是,您可以在请求的标头中发送大小(或多个尺寸)。这可能会达到你想要的效果。另一种方法是串行系统所做的:简单地连接大小(或多个大小)和缓冲区。

size [4 bytes = Int32] + buffer [n bytes]

最后解析第一个字节来读取大小并从缓冲区的第一个字节开始块复制(不要忘记偏移量)。在上面的示例中,您应该从字节号5开始块复制