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

时间:2016-09-26 00:37:33

标签: c# arrays byte type-conversion

我已经获取了一个UInt16值的2D数组,并将其转换为原始字节。我想取这些字节并将它们转换回原始的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]);
}

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

2 个答案:

答案 0 :(得分:2)

所以...不确定你的规格是什么,但是你可以将数组的维度(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();

答案 1 :(得分:1)

您无法获得原始尺寸。例如:

8个字节= [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开始块复制)