Пример #1
0
        protected float[] constructFloatArray(int machinecode, byte[] data)
        {
            var result = new float[data.Length / 4];

            if (machinecode == 15)
            {
                for (int i = 0; i < result.Length; i++)
                {
                    result[i] = PickleUtils.bytes_bigendian_to_float(data, i * 4);
                }
            }
            else
            {
                var bigendian = new byte[4];
                for (int i = 0; i < result.Length; i++)
                {
                    // 14=little 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_bigendian_to_float(bigendian, 0);
                }
            }
            return(result);
        }
Пример #2
0
        public void TestBytes_to_float()
        {
            try {
                PickleUtils.bytes_bigendian_to_float(new byte[] {}, 0);
                Assert.True(false, "expected PickleException");
            } catch (PickleException) {}
            try {
                PickleUtils.bytes_bigendian_to_float(new byte[] { 0 }, 0);
                Assert.True(false, "expected PickleException");
            } catch (PickleException) {}
            Assert.True(0.0f == PickleUtils.bytes_bigendian_to_float(new byte[] { 0, 0, 0, 0 }, 0));
            Assert.True(1.0f == PickleUtils.bytes_bigendian_to_float(new byte[] { 0x3f, 0x80, 0, 0 }, 0));
            Assert.True(1.1f == PickleUtils.bytes_bigendian_to_float(new byte[] { 0x3f, 0x8c, 0xcc, 0xcd }, 0));
            Assert.True(1234.5678f == PickleUtils.bytes_bigendian_to_float(new byte[] { 0x44, 0x9a, 0x52, 0x2b }, 0));
            Assert.True(float.PositiveInfinity == PickleUtils.bytes_bigendian_to_float(new byte[] { 0x7f, 0x80, 0, 0 }, 0));
            Assert.True(float.NegativeInfinity == PickleUtils.bytes_bigendian_to_float(new byte[] { 0xff, 0x80, 0, 0 }, 0));

            // test offset
            Assert.True(1234.5678f == PickleUtils.bytes_bigendian_to_float(new byte[] { 0, 0, 0, 0x44, 0x9a, 0x52, 0x2b }, 3));
            Assert.True(1234.5678f == PickleUtils.bytes_bigendian_to_float(new byte[] { 0x44, 0x9a, 0x52, 0x2b, 0, 0, 0 }, 0));
        }