2016-09-26 56 views
0

我已經採用了二維數組的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調用發送這些字節,並且接收端只會將字節轉換回原始的二維數組。

回答

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

您無法獲得原始尺寸。例如:

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個字節]

終於解析所述第一讀取字節的大小和塊複製從緩衝區1下第一個字節開始(不要忘記的偏移。在上面的例子應該從字節5開始塊複製)

相關問題