Exemple #1
0
        public void Write(float value, IMsgPackWriter writer)
        {
            var binary = new FloatBinary(value);

            byte[] bytes;
            if (BitConverter.IsLittleEndian)
            {
                bytes = new[]
                {
                    (byte)DataTypes.Single,
                    binary.byte3,
                    binary.byte2,
                    binary.byte1,
                    binary.byte0
                };
            }
            else
            {
                bytes = new[]
                {
                    (byte)DataTypes.Single,
                    binary.byte0,
                    binary.byte1,
                    binary.byte2,
                    binary.byte3
                };
            }

            writer.Write(bytes);
        }
Exemple #2
0
        public static bool TryReadFixFloat32(ReadOnlySpan <byte> buffer, out float value, out int readSize)
        {
            readSize = DataLengths.Float32;
            var result = buffer[0] == DataCodes.Float32;
            var binary = new FloatBinary(buffer.Slice(1));

            value = binary.Value;
            return(result);
        }
Exemple #3
0
        public void Write(float value, IMsgPackWriter writer, MsgPackContext context)
        {
            var binary = new FloatBinary(value);

            writer.Write(DataTypes.Single);
            if (BitConverter.IsLittleEndian)
            {
                writer.Write(binary.byte3);
                writer.Write(binary.byte2);
                writer.Write(binary.byte1);
                writer.Write(binary.byte0);
            }
            else
            {
                writer.Write(binary.byte0);
                writer.Write(binary.byte1);
                writer.Write(binary.byte2);
                writer.Write(binary.byte3);
            }
        }
Exemple #4
0
        public static int WriteFixFloat32(Span <byte> buffer, float value)
        {
            var binary = new FloatBinary(value);

            if (BitConverter.IsLittleEndian)
            {
                buffer[4] = binary.Byte0;
                buffer[3] = binary.Byte1;
                buffer[2] = binary.Byte2;
                buffer[1] = binary.Byte3;
            }
            else
            {
                buffer[4] = binary.Byte3;
                buffer[3] = binary.Byte2;
                buffer[2] = binary.Byte1;
                buffer[1] = binary.Byte0;
            }
            buffer[0] = DataCodes.Float32;

            return(DataLengths.Float32);
        }