将byte []转换为原始2d数组 [英] Convert byte[] to original 2d array

查看:58
本文介绍了将byte []转换为原始2d数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将UInt16值的2D数组转换为原始字节。我想获取这些字节并将其转换回原始的2D数组,但是我不确定当我只有字节时如何执行此操作,即是否有一种方法可以确定所有原始字节的尺寸您有将该数组转换为字节吗?

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?

这是我的代码:

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数组。

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.

推荐答案

所以...不确定确切的规格是什么,但是您可以将数组的尺寸(x,y)作为缓冲区的前四个字节发送。下面是我的破解。我对此发表了强烈评论,希望在那里应该有意义。询问该代码是否不清楚。

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

这篇关于将byte []转换为原始2d数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆