Esempio n. 1
0
        public override BigInteger ToInteger()
        {
            if (!(_value.StartsWith(Bytes.HexPrefix) || _value.StartsWith('-' + Bytes.HexPrefix)))
            {
                throw new RpcValueException("The value is not hex string.");
            }

            try
            {
                //The dark magic with 0 and f explained in the article below:
                //https://stackoverflow.com/questions/30119174/converting-a-hex-string-to-its-biginteger-equivalent-negates-the-value

                var sign = "0";

                if (_value[0] == '-')
                {
                    sign   = "";// value.Substring(0, 1);
                    _value = _value.Substring(1);
                }

                var result      = "0" + Bytes.CleanHexPrefix(_value);
                var parseResult = BigInteger.Parse(result, NumberStyles.AllowHexSpecifier);

                if (string.IsNullOrEmpty(sign))
                {
                    parseResult *= -1;
                }

                return(parseResult);
            }
            catch (Exception)
            {
                throw new RpcValueException("The value is not hex string.");
            }
        }