示例#1
0
        public override CompileResult Execute(IEnumerable <FunctionArgument> arguments, ParsingContext context)
        {
            ValidateArguments(arguments, 1);
            var number  = ArgToInt(arguments, 0);
            var padding = default(int?);

            if (arguments.Count() > 1)
            {
                padding = ArgToInt(arguments, 1);
                if (padding.Value < 0 ^ padding.Value > 10)
                {
                    return(CreateResult(eErrorType.Num));
                }
            }
            var result = Convert.ToString(number, 2);

            if (padding.HasValue)
            {
                result = PaddingHelper.EnsureLength(result, padding.Value, "0");
            }
            else
            {
                result = PaddingHelper.EnsureMinLength(result, 10);
            }
            return(CreateResult(result, DataType.String));
        }
示例#2
0
        public override CompileResult Execute(IEnumerable <FunctionArgument> arguments, ParsingContext context)
        {
            ValidateArguments(arguments, 1);
            var number  = ArgToString(arguments, 0);
            var padding = default(int?);

            if (arguments.Count() > 1)
            {
                padding = ArgToInt(arguments, 1);
                if (padding.Value < 0 ^ padding.Value > 10)
                {
                    return(CreateResult(eErrorType.Num));
                }
            }
            if (number.Length > 10)
            {
                return(CreateResult(eErrorType.Num));
            }
            var octStr = string.Empty;

            if (number.Length < 10)
            {
                var n = Convert.ToInt32(number, 2);
                octStr = Convert.ToString(n, 8);
            }
            else
            {
                if (!BinaryHelper.TryParseBinaryToDecimal(number, 2, out int result))
                {
                    return(CreateResult(eErrorType.Num));
                }
                octStr = Convert.ToString(result, 8);
            }
            if (padding.HasValue)
            {
                octStr = PaddingHelper.EnsureLength(octStr, 10, "0");
            }
            return(CreateResult(octStr, DataType.String));
        }
示例#3
0
        public override CompileResult Execute(IEnumerable <FunctionArgument> arguments, ParsingContext context)
        {
            ValidateArguments(arguments, 1);
            var number       = ArgToString(arguments, 0);
            var formatString = "X";

            if (arguments.Count() > 1)
            {
                var padding = ArgToInt(arguments, 1);
                if (padding < 0 ^ padding > 10)
                {
                    return(CreateResult(eErrorType.Num));
                }
                formatString += padding;
            }
            if (number.Length > 10)
            {
                return(CreateResult(eErrorType.Num));
            }
            if (number.Length < 10)
            {
                var n = Convert.ToInt32(number, 2);
                return(CreateResult(n.ToString(formatString), DataType.Decimal));
            }
            else
            {
                if (!BinaryHelper.TryParseBinaryToDecimal(number, 2, out int result))
                {
                    return(CreateResult(eErrorType.Num));
                }
                var hexStr = result.ToString(formatString);
                if (result < 0)
                {
                    hexStr = PaddingHelper.EnsureLength(hexStr, 10, "F");
                }
                return(CreateResult(hexStr, DataType.String));
            }
        }