Exemple #1
0
        private int WriteField(Context context, FloatingPoint field, object fieldValue)
        {
            int num2;

            byte[] buffer = context.Buffer;
            int    num    = field.LengthSpecified ? field.Length : 4;
            string str    = (field.FloatFormat != null) ? field.FloatFormat : context.FloatFormat;

            if (field.GetType() == typeof(Opc.Cpx.Single))
            {
                num = 4;
                str = "IEEE-754";
            }
            else if (field.GetType() == typeof(Opc.Cpx.Double))
            {
                num = 8;
                str = "IEEE-754";
            }
            if (buffer == null)
            {
                return(num);
            }
            if ((buffer.Length - context.Index) < num)
            {
                throw new InvalidDataToWriteException("Unexpected end of buffer.");
            }
            byte[] bytes = null;
            if (str == "IEEE-754")
            {
                switch (num)
                {
                case 4:
                    bytes = BitConverter.GetBytes(Convert.ToSingle(fieldValue));
                    goto Label_00D1;

                case 8:
                    bytes = BitConverter.GetBytes(Convert.ToDouble(fieldValue));
                    goto Label_00D1;
                }
                bytes = (byte[])fieldValue;
            }
            else
            {
                bytes = (byte[])fieldValue;
            }
Label_00D1:
            num2 = 0;
            while (num2 < bytes.Length)
            {
                buffer[context.Index + num2] = bytes[num2];
                num2++;
            }
            return(num);
        }
Exemple #2
0
        private int ReadField(Context context, FloatingPoint field, out object fieldValue)
        {
            fieldValue = null;
            byte[] buffer = context.Buffer;
            int    num    = field.LengthSpecified ? field.Length : 4;
            string str    = (field.FloatFormat != null) ? field.FloatFormat : context.FloatFormat;

            if (field.GetType() == typeof(Opc.Cpx.Single))
            {
                num = 4;
                str = "IEEE-754";
            }
            else if (field.GetType() == typeof(Opc.Cpx.Double))
            {
                num = 8;
                str = "IEEE-754";
            }
            if ((buffer.Length - context.Index) < num)
            {
                throw new InvalidDataInBufferException("Unexpected end of buffer.");
            }
            byte[] buffer2 = new byte[num];
            for (int i = 0; i < num; i++)
            {
                buffer2[i] = buffer[context.Index + i];
            }
            if (str == "IEEE-754")
            {
                switch (num)
                {
                case 4:
                    fieldValue = BitConverter.ToSingle(buffer2, 0);
                    return(num);

                case 8:
                    fieldValue = BitConverter.ToDouble(buffer2, 0);
                    return(num);
                }
                fieldValue = buffer2;
                return(num);
            }
            fieldValue = buffer2;
            return(num);
        }