将 byte[] 转换为原始二维数组
Posted
技术标签:
【中文标题】将 byte[] 转换为原始二维数组【英文标题】:Convert byte[] to original 2d array 【发布时间】:2017-02-03 05:29:59 【问题描述】:我采用了 UInt16 值的二维数组,并将其转换为原始字节。我想获取这些字节并将它们转换回原始的二维数组,但是当我只有字节时我不确定如何执行此操作,即有没有办法确定原始数组的维度当所有你有那个数组转换成字节吗?
这是我的代码:
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 调用发送这些字节,而接收方只有这些字节可以转换回原始二维数组。
【问题讨论】:
【参考方案1】:所以...不确定您的规格是什么,但您可以将数组的维度 (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();
【讨论】:
【参考方案2】:您无法获得原始尺寸。示例:
8 字节 = [0, 1, 0, 2, 0, 1, 0, 2]
到 16 位(2 个字节)的数组中: = [1, 2, 1, 2]
到 64 位(4 字节)的数组中: = [65538, 65538]
所有这些方式(1字节,2字节,4字节)都对解析有效,所以你必须指明你的原始大小,或者至少其中一个。幸运的是,您可以在请求的标头中发送大小(或大小)。这可能会达到你想要的效果。 另一种方法是串行系统所做的:简单地连接你的大小(或大小)和你的缓冲区。
大小 [4 字节 = Int32] + 缓冲区 [n 字节]
最后解析第一个字节以读取大小并从缓冲区的第一个字节开始块复制(不要忘记偏移量。在上面的示例中,您应该从字节数 5 开始块复制)
【讨论】:
以上是关于将 byte[] 转换为原始二维数组的主要内容,如果未能解决你的问题,请参考以下文章