Exemplo n.º 1
0
        public byte[] ToByteArray()
        {
            byte[] result = AllocateResult();

            byte[] data = UnscaledValue.ToByteArray();

            if (data.Length > result.Length)
            {
                throw new NotSupportedException($"decimal data buffer is {data.Length} but result must fit into {result.Length} bytes");
            }

            Array.Copy(data, result, data.Length);

            result = result.Reverse().ToArray();
            return(result);
        }
Exemplo n.º 2
0
        public byte[] ToByteArray()
        {
            /*
             * Java: https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#toByteArray()
             *
             * Returns a byte array containing the two's-complement representation of this BigInteger. The byte array will be in big-endian byte-order: the most significant byte is in the zeroth element. The array will contain the minimum number of bytes required to represent this BigInteger, including at least one sign bit, which is (ceil((this.bitLength() + 1)/8)). (This representation is compatible with the (byte[]) constructor.)
             *
             * C#:   https://msdn.microsoft.com/en-us/library/system.numerics.biginteger.tobytearray(v=vs.110).aspx
             *
             *
             *  value | C# | Java
             *
             * -1 | [1111 1111] | [1111 1111] - no difference, so maybe buffer size?
             *
             */


            byte[] result = AllocateResult();

            byte[] data = UnscaledValue.ToByteArray();
            if (data.Length > result.Length)
            {
                throw new NotSupportedException($"decimal data buffer is {data.Length} but result must fit into {result.Length} bytes");
            }

            Array.Copy(data, result, data.Length);

            //if value is negative fill the remaining bytes with [1111 1111] i.e. negative flag bit (0xFF)
            if (UnscaledValue.Sign == -1)
            {
                for (int i = data.Length; i < result.Length; i++)
                {
                    result[i] = 0xFF;
                }
            }

            result = result.Reverse().ToArray();
            return(result);
        }