Esempio n. 1
0
        public void testBytes_to_float()
        {
            try {
                PickleUtils.bytes_to_float(new byte[] {}, 0);
                Assert.Fail("expected PickleException");
            } catch (PickleException) {}
            try {
                PickleUtils.bytes_to_float(new byte[] { 0 }, 0);
                Assert.Fail("expected PickleException");
            } catch (PickleException) {}
            Assert.IsTrue(0.0f == PickleUtils.bytes_to_float(new byte[] { 0, 0, 0, 0 }, 0));
            Assert.IsTrue(1.0f == PickleUtils.bytes_to_float(new byte[] { 0x3f, 0x80, 0, 0 }, 0));
            Assert.IsTrue(1.1f == PickleUtils.bytes_to_float(new byte[] { 0x3f, 0x8c, 0xcc, 0xcd }, 0));
            Assert.IsTrue(1234.5678f == PickleUtils.bytes_to_float(new byte[] { 0x44, 0x9a, 0x52, 0x2b }, 0));
            Assert.IsTrue(float.PositiveInfinity == PickleUtils.bytes_to_float(new byte[] { 0x7f, 0x80, 0, 0 }, 0));
            Assert.IsTrue(float.NegativeInfinity == PickleUtils.bytes_to_float(new byte[] { 0xff, 0x80, 0, 0 }, 0));

            // test offset
            Assert.IsTrue(1234.5678f == PickleUtils.bytes_to_float(new byte[] { 0, 0, 0, 0x44, 0x9a, 0x52, 0x2b }, 3));
            Assert.IsTrue(1234.5678f == PickleUtils.bytes_to_float(new byte[] { 0x44, 0x9a, 0x52, 0x2b, 0, 0, 0 }, 0));
        }
Esempio n. 2
0
 protected float[] constructFloatArray(int machinecode, byte[] data)
 {
     float[] result    = new float[data.Length / 4];
     byte[]  bigendian = new byte[4];
     for (int i = 0; i < data.Length / 4; ++i)
     {
         if (machinecode == 14)
         {
             result[i] = PickleUtils.bytes_to_float(data, i * 4);
         }
         else
         {
             // 15=big endian, flip the bytes
             bigendian[0] = data[3 + i * 4];
             bigendian[1] = data[2 + i * 4];
             bigendian[2] = data[1 + i * 4];
             bigendian[3] = data[0 + i * 4];
             result[i]    = PickleUtils.bytes_to_float(bigendian, 0);
         }
     }
     return(result);
 }