Exemplo n.º 1
0
        public byte[] ConvertTo(object value)
        {
            TypeSerializer.CheckArgument <decimal>(value);
            var decimalValue = (decimal)value;

            int[] bits = decimal.GetBits(decimalValue);

            int scale = (bits[3] >> 16) & 31;

            byte[] scaleBytes = BeConverter.GetBytes(scale);

            var bigintBytes = new byte[13]; // 13th byte is for making sure that the number is positive

            Buffer.BlockCopy(bits, 0, bigintBytes, 0, 12);

            var bigInteger = new BigInteger(bigintBytes);

            if (decimalValue < 0)
            {
                bigInteger = -bigInteger;
            }

            bigintBytes = bigInteger.ToByteArray();
            Array.Reverse(bigintBytes);

            var resultBytes = new byte[scaleBytes.Length + bigintBytes.Length];

            Array.Copy(scaleBytes, resultBytes, scaleBytes.Length);
            Array.Copy(bigintBytes, 0, resultBytes, scaleBytes.Length, bigintBytes.Length);
            return(resultBytes);
        }
 public byte[] ConvertTo(object value)
 {
     TypeSerializer.CheckArgument <byte[]>(value);
     return((byte[])value);
 }
 public byte[] ConvertTo(object value)
 {
     TypeSerializer.CheckArgument <BigInteger>(value);
     return(((BigInteger)value).ToByteArray());
 }