Esempio n. 1
0
        /// <summary>
        ///   Writes the provided value value as a tagged decimal fraction encoding,
        ///   as described in RFC7049 section 2.4.3
        /// </summary>
        /// <param name="value">The value to write.</param>
        /// <exception cref="InvalidOperationException">
        ///   Writing a new value exceeds the definite length of the parent data item. -or-
        ///   The major type of the encoded value is not permitted in the parent data item. -or-
        ///   The written data is not accepted under the current conformance mode.
        /// </exception>
        public void WriteDecimal(decimal value)
        {
            DecimalHelpers.Deconstruct(value, out decimal mantissa, out byte scale);

            WriteTag(CborTag.DecimalFraction);
            WriteStartArray(2);
            WriteInt64(-(long)scale);

            if (-1m - ulong.MinValue <= mantissa && mantissa <= ulong.MaxValue)
            {
                if (mantissa >= 0m)
                {
                    WriteUInt64((ulong)mantissa);
                }
                else
                {
                    WriteCborNegativeIntegerRepresentation((ulong)(-1m - mantissa));
                }
            }
            else
            {
                // the mantissa can also be a BigNum
                WriteBigInteger((BigInteger)mantissa);
            }

            WriteEndArray();
        }
Esempio n. 2
0
        public void WriteDecimal(decimal value)
        {
            // implements https://tools.ietf.org/html/rfc7049#section-2.4.3
            DecimalHelpers.Deconstruct(value, out decimal mantissa, out byte scale);

            WriteTag(CborTag.DecimalFraction);
            WriteStartArray(2);
            WriteInt64(-(long)scale);

            if (-1m - ulong.MinValue <= mantissa && mantissa <= ulong.MaxValue)
            {
                if (mantissa >= 0m)
                {
                    WriteUInt64((ulong)mantissa);
                }
                else
                {
                    WriteCborNegativeIntegerEncoding((ulong)(-1m - mantissa));
                }
            }
            else
            {
                // the mantissa can also be a BigNum
                WriteBigInteger((BigInteger)mantissa);
            }

            WriteEndArray();
        }