Пример #1
0
        public void TestBytes_to_double()
        {
            try {
                PickleUtils.bytes_bigendian_to_double(new byte[] {}, 0);
                Assert.True(false, "expected PickleException");
            } catch (PickleException) {}
            try {
                PickleUtils.bytes_bigendian_to_double(new byte[] { 0 }, 0);
                Assert.True(false, "expected PickleException");
            } catch (PickleException) {}
            Assert.Equal(0.0d, PickleUtils.bytes_bigendian_to_double(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }, 0));
            Assert.Equal(1.0d, PickleUtils.bytes_bigendian_to_double(new byte[] { 0x3f, 0xf0, 0, 0, 0, 0, 0, 0 }, 0));
            Assert.Equal(1.1d, PickleUtils.bytes_bigendian_to_double(new byte[] { 0x3f, 0xf1, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a }, 0));
            Assert.Equal(1234.5678d, PickleUtils.bytes_bigendian_to_double(new byte[] { 0x40, 0x93, 0x4a, 0x45, 0x6d, 0x5c, 0xfa, 0xad }, 0));
            Assert.Equal(2.17e123d, PickleUtils.bytes_bigendian_to_double(new byte[] { 0x59, 0x8a, 0x42, 0xd1, 0xce, 0xf5, 0x3f, 0x46 }, 0));
            Assert.Equal(1.23456789e300d, PickleUtils.bytes_bigendian_to_double(new byte[] { 0x7e, 0x3d, 0x7e, 0xe8, 0xbc, 0xaf, 0x28, 0x3a }, 0));
            Assert.Equal(double.PositiveInfinity, PickleUtils.bytes_bigendian_to_double(new byte[] { 0x7f, 0xf0, 0, 0, 0, 0, 0, 0 }, 0));
            Assert.Equal(double.NegativeInfinity, PickleUtils.bytes_bigendian_to_double(new byte[] { 0xff, 0xf0, 0, 0, 0, 0, 0, 0 }, 0));
            try
            {
                PickleUtils.bytes_bigendian_to_double(new byte[] { 200, 50, 25, 100 }, 0);
                Assert.True(false, "expected PickleException");
            } catch (PickleException) {}

            // test offset
            Assert.Equal(1.23456789e300d, PickleUtils.bytes_bigendian_to_double(new byte[] { 0, 0, 0, 0x7e, 0x3d, 0x7e, 0xe8, 0xbc, 0xaf, 0x28, 0x3a }, 3));
            Assert.Equal(1.23456789e300d, PickleUtils.bytes_bigendian_to_double(new byte[] { 0x7e, 0x3d, 0x7e, 0xe8, 0xbc, 0xaf, 0x28, 0x3a, 0, 0, 0 }, 0));
        }
Пример #2
0
        protected double[] constructDoubleArray(int machinecode, byte[] data)
        {
            var result = new double[data.Length / 8];

            if (machinecode == 17)
            {
                for (int i = 0; i < result.Length; i++)
                {
                    result[i] = PickleUtils.bytes_bigendian_to_double(data, i * 8);
                }
            }
            else
            {
                var bigendian = new byte[8];
                for (int i = 0; i < result.Length; i++)
                {
                    // 16=little endian, flip the bytes
                    bigendian[0] = data[7 + i * 8];
                    bigendian[1] = data[6 + i * 8];
                    bigendian[2] = data[5 + i * 8];
                    bigendian[3] = data[4 + i * 8];
                    bigendian[4] = data[3 + i * 8];
                    bigendian[5] = data[2 + i * 8];
                    bigendian[6] = data[1 + i * 8];
                    bigendian[7] = data[0 + i * 8];
                    result[i]    = PickleUtils.bytes_bigendian_to_double(bigendian, 0);
                }
            }
            return(result);
        }