现有一个byte数组,含两个字节(共16位),表示的是一个有符号的整数
怎样计算才能得到这个有符号的整数呢?请指教,急

解决方案 »

  1.   

    int Ret = BitConverter.ToUInt32(bytes,0); 
      

  2.   

    给你一个我用过的代码吧。    public static unsafe Int16 HexToShort(byte[] byteValue)
            {
                Int16[] nValuePtr = new Int16[1];
                int sizeOfValue = Marshal.SizeOf(typeof(Int16));//            sizeOfValue = 1;
                fixed (Int16* pTempValue = nValuePtr)
                {
                    Marshal.Copy(byteValue, 0, new IntPtr(pTempValue), sizeOfValue);
                }            return nValuePtr[0];
            }
      

  3.   

    呵呵,上面的代码也是可以的。最简单是使用
    byte[] ba = new byte[]{15,16};
    Int16 i16 = BitConverter.ToInt16(ba,0);
      

  4.   

     System.BitConverter.ToInt16试试
      

  5.   

    byte[] ba = new byte[]{15,16};
    Int16 i16 = BitConverter.ToInt16(ba,0);
      

  6.   

    Int16就是有符号的。
    UInt16才是无符号的。
      

  7.   

    谢谢phy了,但还想问一下:比如 byte[] bts = new byte[2] {0x80,0x00}
    那么对应的二进制应该是 1000 0000 0000 0000 也就是-32768,为什么用BitConverter.ToInt16(bts,0)得到的是1呢?
    而换成{0x00,0x80},即 0000 0000 1000 0000得到的却是-32768谢谢
      

  8.   

     int Ret = BitConverter.ToInt16(bytes,0);