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

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

            // apply defaults for built in types.
            if (field.GetType() == typeof(Opc.Cpx.Single))
            {
                length = 4; format = Context.FLOAT_FORMAT_IEEE754;
            }
            else if (field.GetType() == typeof(Opc.Cpx.Double))
            {
                length = 8; format = Context.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 InvalidDataToWriteException("Unexpected end of buffer.");
                }

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

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

                    case 8:  { bytes = BitConverter.GetBytes(System.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);
        }
Ejemplo n.º 2
0
        // Token: 0x060003F1 RID: 1009 RVA: 0x0000B604 File Offset: 0x0000A604
        private int WriteField(Context context, FloatingPoint field, object fieldValue)
        {
            byte[] buffer = context.Buffer;
            int    num    = field.LengthSpecified ? field.Length : 4;
            string a      = (field.FloatFormat != null) ? field.FloatFormat : context.FloatFormat;

            if (field.GetType() == typeof(Single))
            {
                num = 4;
                a   = "IEEE-754";
            }
            else if (field.GetType() == typeof(Double))
            {
                num = 8;
                a   = "IEEE-754";
            }
            if (buffer != null)
            {
                if (buffer.Length - context.Index < num)
                {
                    throw new InvalidDataToWriteException("Unexpected end of buffer.");
                }
                byte[] array;
                if (a == "IEEE-754")
                {
                    int num2 = num;
                    if (num2 != 4)
                    {
                        if (num2 != 8)
                        {
                            array = (byte[])fieldValue;
                        }
                        else
                        {
                            array = BitConverter.GetBytes(System.Convert.ToDouble(fieldValue));
                        }
                    }
                    else
                    {
                        array = BitConverter.GetBytes(System.Convert.ToSingle(fieldValue));
                    }
                }
                else
                {
                    array = (byte[])fieldValue;
                }
                for (int i = 0; i < array.Length; i++)
                {
                    buffer[context.Index + i] = array[i];
                }
            }
            return(num);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Reads a floating point value from the buffer.
        /// </summary>
        private int ReadField(Context 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 != null)?field.FloatFormat:context.FloatFormat;

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

            // check if there is enough data left.
            if (buffer.Length - context.Index < length)
            {
                throw new InvalidDataInBufferException("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 == Context.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);
        }
Ejemplo n.º 4
0
        // Token: 0x0600036D RID: 877 RVA: 0x00009D24 File Offset: 0x00008D24
        private int ReadField(Context context, FloatingPoint field, out object fieldValue)
        {
            fieldValue = null;
            byte[] buffer = context.Buffer;
            int    num    = field.LengthSpecified ? field.Length : 4;
            string a      = (field.FloatFormat != null) ? field.FloatFormat : context.FloatFormat;

            if (field.GetType() == typeof(Single))
            {
                num = 4;
                a   = "IEEE-754";
            }
            else if (field.GetType() == typeof(Double))
            {
                num = 8;
                a   = "IEEE-754";
            }
            if (buffer.Length - context.Index < num)
            {
                throw new InvalidDataInBufferException("Unexpected end of buffer.");
            }
            byte[] array = new byte[num];
            for (int i = 0; i < num; i++)
            {
                array[i] = buffer[context.Index + i];
            }
            if (a == "IEEE-754")
            {
                int num2 = num;
                if (num2 != 4)
                {
                    if (num2 != 8)
                    {
                        fieldValue = array;
                    }
                    else
                    {
                        fieldValue = BitConverter.ToDouble(array, 0);
                    }
                }
                else
                {
                    fieldValue = BitConverter.ToSingle(array, 0);
                }
            }
            else
            {
                fieldValue = array;
            }
            return(num);
        }