Exemple #1
0
        public void ConvertBack(object source, byte[] destination, ushort position, ushort length, object parameter)
        {
            if (source == null)
            {
                throw new ArgumentNullException();
            }
            var scale = MathExtentions.PowerOf10((parameter as int?) ?? 0);

            switch (length)
            {
            case 1:
                destination[position] = (byte)(System.Convert.ToDecimal(source) * scale);
                return;

            case 2:
                Array.Copy(BitConverter.GetBytes((ushort)(System.Convert.ToDecimal(source) * scale)), 0, destination, position, length);
                return;

            case 4:
                Array.Copy(BitConverter.GetBytes((uint)(System.Convert.ToDecimal(source) * scale)), 0, destination, position, length);
                return;

            case 8:
                Array.Copy(BitConverter.GetBytes((ulong)(System.Convert.ToDecimal(source) * scale)), 0, destination, position, length);
                return;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Exemple #2
0
        public void ConvertBack(object source, byte[] destination, ushort position, ushort length, object parameter = null)
        {
            if (source == null)
            {
                throw new ArgumentNullException();
            }
            var signible = (byte)0x0f;
            var scale    = MathExtentions.PowerOf10((parameter as int?) ?? 0);
            var value    = Math.Truncate(System.Convert.ToDecimal(source) * scale);

            if (value < 0)
            {
                value    = -value;
                signible = (byte)0x0d;
            }
            var valueString = value.ToString();

            if (valueString.Length > length * 2 - 1)
            {
                throw new ArgumentException();
            }
            var zerofillCount = length * 2 - 1 - valueString.Length;

            if (zerofillCount != 0)
            {
                valueString = new string('0', zerofillCount) + valueString;
            }
            for (var i = 0; i < length; i++)
            {
                var digit = 0;
                digit |= byte.Parse(valueString.Substring(i * 2, 1)) << 4;
                if (i == length - 1)
                {
                    digit |= signible;
                }
                else
                {
                    digit |= byte.Parse(valueString.Substring(i * 2, 1));
                }
                destination[position + i] = (byte)digit;
            }
        }
Exemple #3
0
        public object Convert(byte[] source, ushort position, ushort length, object parameter)
        {
            var scale = MathExtentions.PowerOf10((parameter as int?) ?? 0);

            switch (length)
            {
            case 1:
                return(source[position] / scale);

            case 2:
                return(BitConverter.ToUInt16(source, position) / scale);

            case 4:
                return(BitConverter.ToUInt32(source, position) / scale);

            case 8:
                return(BitConverter.ToUInt64(source, position) / scale);

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Exemple #4
0
        public object Convert(byte[] source, ushort position, ushort length, object parameter)
        {
            var scale  = MathExtentions.PowerOf10((parameter as int?) ?? 0);
            var result = 0m;

            for (var i = 0; i < length; i++)
            {
                if (i == length - 1)
                {
                    result += 1m * (source[position + i] >> 4);
                    if ((source[position + i] & 0x0f) == 0x0d)
                    {
                        result = -result;
                    }
                }
                else
                {
                    result += MathExtentions.PowerOf10((length - i) * 2 - 2) * (source[position + i] >> 4);
                    result += MathExtentions.PowerOf10((length - i) * 2 - 3) * (source[position + i] & 0x0f);
                }
            }
            return(result / scale);
        }