2016-09-22 131 views
-1

我有我已經轉換爲原始字節的UInt16s的二維數組 - 我想採取這些字節並將它們轉換回原始的二維數組。我設法用2d陣列的雙打做到這一點,但我無法弄清楚如何用UInt16做到這一點。將byte []轉換爲UInt16。

這裏是我的代碼:

UInt16[,] dataArray; 
//This array is populated with this data: 
[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(newUint16Array, 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/8]; 
for (int i = 0; i < 5; i++) 
{ 
    originalUInt16Values[i] = BitConverter.ToUInt16(bufferUInt16, i * 8); 
    Console.WriteLine("Values: " + originalUInt16Values[i]); 
} 

print語句不顯示爲原始二維數組相同的值。我對字節和UInt16編碼非常陌生,所以我在這個過程中學到了大部分。另外,我知道我的代碼的最後一塊沒有像原始數組一樣將值放入一個2d數組中 - 現在我只是試圖打印出值來查看它們是否與原始數據匹配。

+0

'/ 8','* 8'-w hy 8而不是sizeof(UInt16)這是2?從'雙'結束:) –

回答

2

如果你想要的僅僅是投UINT16 [,] - >字節,然後針對字節> UINT16可以另做塊拷貝,這是非常快的,在運行時,代碼應該是這樣的:

UInt16[,] dataArray = new UInt16[,] { 
    {4, 6, 2}, 
    {0, 2, 0}, 
    {1, 3, 4} 
}; 
for (int j = 0; j < 3; j++) 
{ 
    for (int i = 0; i < 3; i++) 
    { 
     Console.WriteLine("Value[" + i + ", " + j + "] = " + dataArray[j,i]); 
    } 
} 
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++) 
{ 
    //originalUInt16Values[i] = BitConverter.ToUInt16(bufferUInt16, i * 8); 
    Console.WriteLine("Values---: " + originalUInt16Values[i]); 
} 

順便說一下,你只劃分出的每個UINT16爲兩個字節,所以你應該用兩個計算出你的新的大小劃分,而不是八個

+0

謝謝!完美的作品。你知道字節[]中是否有任何內容會告訴我原始二維數組的尺寸?如果我所有的都是字節,那麼是否有可能知道它們最初來自二維數組?我爲此創建了一個新帖子:http://stackoverflow.com/questions/39693214/convert-byte-to-original-2d-array – Roka545

0

您的投放,以便您應該能夠做的事情隱含

var list = new List<byte> { 1, 2 , 
var uintList = new List<UInt16>(); 

//Cast in your select 
uintList = list.Select(x => (UInt16)x).ToList(); 
2

程序

public static void Main(string[] args) 
    { 
     UInt16[,] dataArray = new ushort[,]{ {4,6,2}, {0,2,0}, {1,3,4}}; 
     //This array is populated with this data: 

     long byteCountUInt16Array = dataArray.GetLength(0) * dataArray.GetLength(1) * sizeof(UInt16); 

     var byteBuffer = new byte[byteCountUInt16Array]; 
     Buffer.BlockCopy(dataArray, 0, byteBuffer, 0, byteBuffer.Length); 

     for(int i=0; i < byteBuffer.Length; i++) { 
      Console.WriteLine("byteBuf[{0}]= {1}", i, byteBuffer[i]); 
     } 


     Console.WriteLine("Byte buffer len: {0} data array len: {1}", byteBuffer.Length, dataArray.GetLength(0)* dataArray.GetLength(1)); 
     UInt16[] originalUInt16Values = new UInt16[byteBuffer.Length/2]; 
     for (int i = 0; i < byteBuffer.Length; i+=2) 
     { 
      ushort _a = (ushort)((byteBuffer[i]) | (byteBuffer[i+1]) << 8); 
      originalUInt16Values[i/2] = _a; 
      Console.WriteLine("Values: " + originalUInt16Values[i/2]); 
     } 
    } 

輸出

byteBuf[0]= 4 
byteBuf[1]= 0 
byteBuf[2]= 6 
byteBuf[3]= 0 
byteBuf[4]= 2 
byteBuf[5]= 0 
byteBuf[6]= 0 
byteBuf[7]= 0 
byteBuf[8]= 2 
byteBuf[9]= 0 
byteBuf[10]= 0 
byteBuf[11]= 0 
byteBuf[12]= 1 
byteBuf[13]= 0 
byteBuf[14]= 3 
byteBuf[15]= 0 
byteBuf[16]= 4 
byteBuf[17]= 0 
Byte buffer len: 18 data array len: 9 
Values: 4 
Values: 6 
Values: 2 
Values: 0 
Values: 2 
Values: 0 
Values: 1 
Values: 3 
Values: 4 

一個ushort,又名UInt16存儲你看在一個字節順序中,其中4 = 0x04 0x00,即i所以我選擇的轉換公式

  ushort _a = (ushort)((byteBuffer[i]) | (byteBuffer[i+1]) << 8); 

哪個將抓住byte在索引i並採取在i+1下一byte和由一個字節(8位),以彌補的16個比特的大小左移它一個ushort。用或者,ushort _a = 0x[second byte] 0x[first byte],然後重複。此轉換代碼特定於您所在機器的永久性,因此不可攜帶。

另外,我修正了byteBuffer陣列因爲它乘以因子8而產生的錯誤。 A ushortbyte的兩倍,因此我們只需要數組長度中的因子2。

1

解決你的問題的標題(轉換字節[]以UINT16):

UInt16 result = (UInt16)BitConverter.ToInt16(yourByteArray, startIndex = 0);