public void ToXXX()
        {
            var sut = new LittleEndianBitConverter();

            var data = new byte[] { 0x03, 0, 0, 0, 0, 0, 0, 0 };

            Check.That(sut.ToBoolean(data, 0)).IsTrue();
            Check.That(sut.ToBoolean(data, 7)).IsFalse();
            Check.That(sut.ToChar(data, 0)).IsEqualTo('\u0003');
            Check.That(sut.ToChar(data, 6)).IsEqualTo('\0');
            Check.That(sut.ToInt16(data, 0)).IsEqualTo(3);
            Check.That(sut.ToInt16(data, 6)).IsEqualTo(0);
            Check.That(sut.ToUInt16(data, 0)).IsEqualTo(3u);
            Check.That(sut.ToUInt16(data, 6)).IsEqualTo(0u);
            Check.That(sut.ToInt32(data, 0)).IsEqualTo(3);
            Check.That(sut.ToInt32(data, 4)).IsEqualTo(0);
            Check.That(sut.ToUInt32(data, 0)).IsEqualTo(3u);
            Check.That(sut.ToUInt32(data, 4)).IsEqualTo(0u);
            Check.That(sut.ToInt64(data, 0)).IsEqualTo(3L);
            Check.That(sut.ToUInt64(data, 0)).IsEqualTo(3UL);

            data = new byte[] { 0, 0, 0, 0, 0, 0, 0x20, 0x41 };
            Check.That(sut.ToSingle(data, 0)).IsEqualTo(0.0f);
            Check.That(sut.ToSingle(data, 4)).IsEqualTo(10.0f);

            data = new byte[] { 0, 0, 0, 0, 0, 0, 0x24, 0x40 };
            Check.That(sut.ToDouble(data, 0)).IsEqualTo(10.0);
        }
        public void LittleEndianBitConverter_Single()
        {
            const int n = sizeof(Single);

            const Single pos  = (float)Math.PI;
            const Single zero = 0;
            const Single neg  = -pos;

            var posRepr = GetCanonicalRepresentation(pos);
            var negRepr = GetCanonicalRepresentation(neg);

            // --------------------------------------------------------------

            var buffer = new byte[n];

            LittleEndianBitConverter.FillBytes(pos, buffer);
            Assert.IsTrue(buffer.SequenceEqual(posRepr));
            Assert.IsTrue(LittleEndianBitConverter.GetBytes(pos).SequenceEqual(buffer));
            Assert.AreEqual(pos, LittleEndianBitConverter.ToSingle(buffer));

            LittleEndianBitConverter.FillBytes(zero, buffer);
            Assert.IsTrue(buffer.SequenceEqual(new byte[n]));
            Assert.IsTrue(LittleEndianBitConverter.GetBytes(zero).SequenceEqual(buffer));
            Assert.AreEqual(zero, LittleEndianBitConverter.ToSingle(buffer));

            LittleEndianBitConverter.FillBytes(neg, buffer);
            Assert.IsTrue(buffer.SequenceEqual(negRepr));
            Assert.IsTrue(LittleEndianBitConverter.GetBytes(neg).SequenceEqual(buffer));
            Assert.AreEqual(neg, LittleEndianBitConverter.ToSingle(buffer));
        }
        public float DecodeFloat32(byte[] bytes, Iterator it)
        {
            float value = bitConverter.ToSingle(bytes, it.Offset);

            it.Offset += 4;
            return(value);
        }
Exemple #4
0
 private static float smethod_3(Stream stream, byte[] buffer)
 {
     stream.Read(buffer, 0, 4);
     return(LittleEndianBitConverter.ToSingle(buffer));
 }
Exemple #5
0
        /// <summary>
        /// Read data from stream
        /// </summary>
        /// <param name="s">stream</param>
        /// <returns>data of specified type</returns>
        public static float ToSingle(Stream s)
        {
            byte[] buf = GetBytes(s, sizeof(ulong));

            return(LittleEndianBitConverter.ToSingle(buf, 0));
        }