Example #1
0
        /// <summary>
        /// Encode <b>RawInt128</b> value provided to array of int values
        /// representing <b>Decimal</b> unscaled value bits.
        /// </summary>
        /// <param name="int128">
        /// <b>RawInt128</b> value.
        /// </param>
        /// <returns>
        /// Decimal unscaled value bits as array of int.
        /// </returns>
        public static int[] EncodeDecimalBits(RawInt128 int128)
        {
            int[]  result = new int[4];
            byte[] rawInt = CollectionUtils.ToByteArrayUnchecked(int128.Value);
            byte[] bits   = new byte[4];
            int    fourth = 0;
            int    bcount = 0;

            for (int i = rawInt.Length - 1; i > -1; i--)
            {
                fourth++;
                bits[fourth - 1] = rawInt[i];

                if (fourth == 4)
                {
                    int intval = BitConverter.ToInt32(bits, 0);
                    result[bcount++] = intval;
                    bits             = new byte[4];
                    fourth           = 0;
                }
            }

            if (fourth > 0 && bcount < result.Length)
            {
                result[bcount] = BitConverter.ToInt32(bits, 0);
            }

            return(result);
        }
        /// <summary>
        /// Write a <b>RawInt128</b> value to <b>DataWriter</b>.
        /// </summary>
        /// <remarks>
        /// <b>RawInt128</b> value, which is represented as array of
        /// signed bytes.
        /// </remarks>
        /// <param name="writer">
        /// The DataWriter to write to.
        /// </param>
        /// <param name="rawInt128">
        /// <b>RawInt128</b> value.
        /// </param>
        public virtual void WritePackedRawInt128(DataWriter writer, RawInt128 rawInt128)
        {
            sbyte[] bigInteger = new sbyte[rawInt128.Length];
            Buffer.BlockCopy(rawInt128.Value, 0, bigInteger, 0, rawInt128.Length);

            int b     = 0;
            int cBits = bigInteger.Length * 8;
            int cb    = bigInteger.Length;

            // check for negative
            if (rawInt128.IsNegative)
            {
                b = 0x40;
                for (int of = 0; of < cb; ++of)
                {
                    bigInteger[of] = (sbyte)~bigInteger[of];
                }
            }

            int ofMSB = 0;

            while (ofMSB < cb && bigInteger[ofMSB] == 0)
            {
                ++ofMSB;
            }

            if (ofMSB < cb)
            {
                int of    = cb - 1;
                int nBits = bigInteger[of] & 0xFF;

                b    |= (sbyte)(nBits & 0x3F);
                nBits = NumberUtils.URShift(nBits, 6);

                cBits = 2;  // only 2 data bits left in nBits

                while (nBits != 0 || of > ofMSB)
                {
                    b |= 0x80;
                    writer.Write((sbyte)b);

                    // load more data bits if necessary
                    if (cBits < 7)
                    {
                        nBits |= (--of < 0 ? 0 : bigInteger[of] & 0xFF) << cBits;
                        cBits += 8;
                    }

                    b      = (nBits & 0x7F);
                    nBits  = NumberUtils.URShift(nBits, 7);
                    cBits -= 7;
                }
            }

            writer.Write((sbyte)b);
        }
Example #3
0
        private void writeInt128()
        {
            DataWriter        dw  = initPofWriter("Int128");
            WritingPofHandler wfh = new WritingPofHandler(dw);
            PofStreamWriter   psw = new PofStreamWriter(wfh, new SimplePofContext());

            // Test data:
            //   First byte array is equivalent to BigInteger.TEN
            byte[] b1 = { 0x0a };

            //   Second byte array is equivalent to BigInteger("55 5555 5555 5555 5555")
            //   (spaces are there for human parsing).
            //   Equivalent to hex: 0x 07 b5 ba d5 95 e2 38 e3
            byte[] b2 = { 0x07, 0xb5, 0xba, 0xd5, 0x95, 0xe2, 0x38, 0xe3 };

            RawInt128 rawInt1 = new RawInt128(b1);
            RawInt128 rawInt2 = new RawInt128(b2);

            psw.WriteRawInt128(0, rawInt1);
            psw.WriteRawInt128(0, rawInt2);

            dw.Flush();
            dw.Close();
        }