public float ReadFloat32() { if (stream.Read(buffer, 0, 4) == 4) { return(Float32Bits.ToSingle(buffer)); } throw new FormatException(); }
internal static void GetBytes(float value, byte[] buffer) { Float32Bits bits = new Float32Bits(value); if (BitConverter.IsLittleEndian) { buffer[0] = bits.byte3; buffer[1] = bits.byte2; buffer[2] = bits.byte1; buffer[3] = bits.byte0; } else { buffer[0] = bits.byte0; buffer[1] = bits.byte1; buffer[2] = bits.byte2; buffer[3] = bits.byte3; } }
internal static float ToSingle(byte[] bigEndianBytes) { Float32Bits bits = default(Float32Bits); if (BitConverter.IsLittleEndian) { bits.byte0 = bigEndianBytes[3]; bits.byte1 = bigEndianBytes[2]; bits.byte2 = bigEndianBytes[1]; bits.byte3 = bigEndianBytes[0]; } else { bits.byte0 = bigEndianBytes[0]; bits.byte1 = bigEndianBytes[1]; bits.byte2 = bigEndianBytes[2]; bits.byte3 = bigEndianBytes[3]; } return(bits.value); }
public void Write(float value) { WriteFormat(Format.Float32); Float32Bits.GetBytes(value, buffer); stream.Write(buffer, 0, 4); }