Exemplo n.º 1
0
        public static double ParseHexFloat(string s)
        {
            try
            {
                if ((s.Length < 2) || (s[0] != '0' && (char.ToUpper(s[1]) != 'X')))
                {
                    throw new InternalErrorException("hex float must start with '0x' near '{0}'", s);
                }

                s = s.Substring(2);

                double value = 0.0;
                int    dummy, exp = 0;

                s = LexerUtils.ReadHexProgressive(s, ref value, out dummy);

                if (s.Length > 0 && s[0] == '.')
                {
                    s = s.Substring(1);
                    s = LexerUtils.ReadHexProgressive(s, ref value, out exp);
                }

                exp *= -4;

                if (s.Length > 0 && char.ToUpper(s[0]) == 'P')
                {
                    if (s.Length == 1)
                    {
                        throw new InternalErrorException("invalid hex float format near '{0}'", s);
                    }

                    s = s.Substring(s[1] == '+' ? 2 : 1);

                    int exp1 = int.Parse(s, CultureInfo.InvariantCulture);

                    exp += exp1;
                }

                double result = value * Math.Pow(2, exp);

                return(result);
            }
            catch (FormatException)
            {
                throw new InternalErrorException("malformed number near '{0}'", s);
            }
        }