2016-09-26 3 views
0

2 차원 배열의 UInt16 값을 원시 바이트로 변환했습니다. 그 바이트를 가져 와서 원래의 2D 배열로 다시 변환하고 싶습니다만, 바이트 만 가질 때 이것을 수행하는 방법이 확실하지 않습니다. 즉, 원래 배열의 크기를 결정할 수있는 방법이 있습니다. 배열을 바이트로 변환 했습니까?byte []를 원래의 2d 배열로 변환

여기 내 코드입니다 :이 코드는 1 차원 배열에 바이트를 넣어 것입니다

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

,하지만 난 원래 2 차원 배열에 넣어 싶습니다. 이 모든 것이 원시 바이트 인 경우 가능합니까? 결국 REST 호출을 통해이 바이트를 보낼 것이며 수신 측은 원래 2D 배열로 다시 변환 할 바이트 만 갖게됩니다.

답변

2

그래서 ... 정확히 어떤 사양인지는 모르겠지만 배열의 크기 (x, y)를 버퍼의 처음 4 바이트로 보낼 수 있습니다. 아래는 내 균열이다. 나는 그것을 크게 논평했다. 희망적으로 거기에 의미가 있어야한다. 해당 코드가 명확하지 않은 경우 질문하십시오.

/**** 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 바이트)의 배열

: = 65,538, 65,538]

이러한 방법으로는 표시해야하므로 (1 바이트, 2 바이트, 4 바이트)는, 분석을위한 유효한 모든 원래 크기 또는 적어도 하나의 크기. 다행히도 요청의 헤더에 크기 (또는 크기)를 보낼 수 있습니다. 이것은 당신이 원하는 것을 속일 수 있습니다. 이 작업을 수행하는 또 다른 방법은 직렬 시스템에서 수행하는 작업입니다. 크기 (또는 크기)와 버퍼를 연결하기 만하면됩니다.

크기 [4 바이트 = INT32] + 완충액 [n 바이트]

최종적 완충액 1 첫번째 바이트부터 크기 및 블록 사본을 읽는 첫 바이트를 파싱 (오프셋을 잊지 말고. 년 위의 예는 바이트 번호 5에서 블록 복사를 시작해야합니다.