2016-09-22 129 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);