// Parse an IEEE-754 decimal format to a FbDecFloat. public FbDecFloat ParseBytes(byte[] decBytes) { // this (and related) code works with BE if (BitConverter.IsLittleEndian) { Array.Reverse(decBytes); } _decimalFormat.ValidateByteLength(decBytes); var firstByte = decBytes[0] & 0xff; var signum = -1 * UnsignedRightShift(firstByte, 7) | 1; var decimalType = DecimalTypeFromFirstByte(firstByte); switch (decimalType) { case DecimalType.Infinity: return(signum == NegativeSignum ? FbDecFloat.NegativeInfinity : FbDecFloat.PositiveInfinity); case DecimalType.NaN: return(signum == NegativeSignum ? FbDecFloat.NegativeNaN : FbDecFloat.PositiveNaN); case DecimalType.SignalingNaN: return(signum == NegativeSignum ? FbDecFloat.NegativeSignalingNaN : FbDecFloat.PositiveSignalingNaN); case DecimalType.Finite: { // NOTE: get exponent MSB from combination field and first 2 bits of exponent continuation in one go int exponentMSB; int firstDigit; if ((firstByte & Combination2) != Combination2) { exponentMSB = UnsignedRightShift(firstByte, 3) & 0b01100 | (firstByte & 0b011); firstDigit = UnsignedRightShift(firstByte, 2) & 0b0111; } else { exponentMSB = UnsignedRightShift(firstByte, 1) & 0b01100 | (firstByte & 0b011); firstDigit = 0b01000 | (UnsignedRightShift(firstByte, 2) & 0b01); } var exponentBitsRemaining = _decimalFormat.ExponentContinuationBits - 2; Debug.Assert(exponentBitsRemaining == _decimalFormat.FormatBitLength - 8 - _decimalFormat.CoefficientContinuationBits, $"Unexpected exponent remaining length {exponentBitsRemaining}."); var exponent = _decimalFormat.UnbiasedExponent(DecodeExponent(decBytes, exponentMSB, exponentBitsRemaining)); var coefficient = _coefficientCoder.DecodeValue(signum, firstDigit, decBytes); return(new FbDecFloat(DecimalType.Finite, signum == NegativeSignum, coefficient, exponent)); } default: throw new ArgumentOutOfRangeException(); } }