Esempio n. 1
0
        /// <summary>
        /// Writes a integer value from to the buffer.
        /// </summary>
        private int WriteField(TsCCpxContext context, FloatingPoint field, object fieldValue)
        {
            byte[] buffer = context.Buffer;

            // initialize serialization paramters.
            int    length = (field.LengthSpecified) ? (int)field.Length : 4;
            string format = field.FloatFormat ?? context.FloatFormat;

            // apply defaults for built in types.
            if (field.GetType() == typeof(Single))
            {
                length = 4; format = TsCCpxContext.FLOAT_FORMAT_IEEE754;
            }
            else if (field.GetType() == typeof(Double))
            {
                length = 8; format = TsCCpxContext.FLOAT_FORMAT_IEEE754;
            }

            // only write to the buffer if it has been allocated.
            if (buffer != null)
            {
                // check if there is enough data left.
                if (buffer.Length - context.Index < length)
                {
                    throw new TsCCpxInvalidDataToWriteException("Unexpected end of buffer.");
                }

                // copy bytes if required.
                byte[] bytes = null;

                if (format == TsCCpxContext.FLOAT_FORMAT_IEEE754)
                {
                    switch (length)
                    {
                    case 4: { bytes = BitConverter.GetBytes(Convert.ToSingle(fieldValue)); break; }

                    case 8: { bytes = BitConverter.GetBytes(Convert.ToDouble(fieldValue)); break; }

                    default: { bytes = (byte[])fieldValue; break; }
                    }
                }
                else
                {
                    bytes = (byte[])fieldValue;
                }

                // write bytes to buffer.
                for (int ii = 0; ii < bytes.Length; ii++)
                {
                    buffer[context.Index + ii] = bytes[ii];
                }
            }

            return(length);
        }
        /// <summary>
        /// Reads a floating point value from the buffer.
        /// </summary>
        private int ReadField(TsCCpxContext context, FloatingPoint field, out object fieldValue)
        {
            fieldValue = null;

            byte[] buffer = context.Buffer;

            // initialize serialization paramters.
            int    length = (field.LengthSpecified) ? (int)field.Length : 4;
            string format = field.FloatFormat ?? context.FloatFormat;

            // apply defaults for built in types.
            if (field.GetType() == typeof(Single))
            {
                length = 4; format = TsCCpxContext.FLOAT_FORMAT_IEEE754;
            }
            else if (field.GetType() == typeof(Double))
            {
                length = 8; format = TsCCpxContext.FLOAT_FORMAT_IEEE754;
            }

            // check if there is enough data left.
            if (buffer.Length - context.Index < length)
            {
                throw new TsCCpxInvalidDataInBufferException("Unexpected end of buffer.");
            }

            // copy bytes.
            byte[] bytes = new byte[length];

            for (int ii = 0; ii < length; ii++)
            {
                bytes[ii] = buffer[context.Index + ii];
            }

            // convert to object.
            if (format == TsCCpxContext.FLOAT_FORMAT_IEEE754)
            {
                switch (length)
                {
                case 4: { fieldValue = BitConverter.ToSingle(bytes, 0); break; }

                case 8: { fieldValue = BitConverter.ToDouble(bytes, 0); break; }

                default: { fieldValue = bytes; break; }
                }
            }
            else
            {
                fieldValue = bytes;
            }

            return(length);
        }