public static double BytesToDoubleCalc(byte[] buffer, int offset, bool isLittleEndian)
        {
            byte[] subBuffer = new byte[8];
            Array.Copy(buffer, offset, subBuffer, 0, 8);

            if (BytesToInt64(subBuffer, 0, true) == 0)
            {
                return(0);
            }

            if (!isLittleEndian)
            {
                subBuffer = BitUtil.ReverseBuffer(subBuffer);
            }

            byte tmp        = subBuffer[0];
            byte sign       = (byte)(tmp >> 7);
            int  exp_upper7 = tmp << 4;

            tmp = subBuffer[1];
            int    exp_lower4 = tmp >> 4;
            int    exponent   = exp_upper7 | exp_lower4;
            int    _exponent  = exponent - 1023;
            int    bytepos    = 1;
            int    bitpos     = 3;
            string binary     = (String)BitUtil.ReadBits(subBuffer, ref bytepos, ref bitpos, 52, true);

            if (exponent == 2047)
            {
                if (StringMath.are_equal(binary, "0"))
                {
                    if (sign == 1)
                    {
                        return(double.NegativeInfinity);
                    }
                    else
                    {
                        return(double.PositiveInfinity);
                    }
                }
                else
                {
                    return(double.NaN);
                }
            }
            else
            {
                binary = StringMath.beautify_number("1." + binary);
                double dec;
                dec  = Convert.ToDouble(BaseConverter.Convert(binary, 2, 10, 0, 31));
                dec *= 1 - (2 * sign);
                dec *= Math.Pow(2, _exponent);
                return(dec);
            }
        }
Esempio n. 2
0
        public static string GetBaseFromValue(string value, int numberBase, int precision)
        {
            bool isNegative = false;

            if (StringMath.is_greater("0", value))
            {
                value      = StringMath.switch_sign(value);
                isNegative = true;
            }

            string baseNumber        = "";
            string integer_part      = StringMath.get_integer_part(value);
            string fraction_part     = "0." + StringMath.get_fraction_part(value);
            string timesForNextDigit = integer_part;
            string remainderForCurrentDigit;
            char   c;

            do
            {
                remainderForCurrentDigit = StringMath.remainder(timesForNextDigit, numberBase.ToString());
                timesForNextDigit        = StringMath.divide(new List <string> {
                    timesForNextDigit, numberBase.ToString()
                }, 0);
                c          = GetBaseDigitForValue(System.Convert.ToInt32(remainderForCurrentDigit));
                baseNumber = c + baseNumber;
            } while (!StringMath.are_equal(timesForNextDigit, "0"));

            int _precision = 0;


            for (int e = 0; true; e--)
            {
                if (_precision == precision)
                {
                    break;
                }

                if (StringMath.are_equal(fraction_part, "0"))
                {
                    break;
                }
                else if (e == 0)
                {
                    baseNumber += ".";
                }

                fraction_part = StringMath.multiply(new List <string> {
                    fraction_part, numberBase.ToString()
                });
                string digitValue = StringMath.get_integer_part(fraction_part);

                if (StringMath.is_greater_or_equal(digitValue, "1"))
                {
                    fraction_part = StringMath.subtract(new List <string> {
                        fraction_part, digitValue
                    });
                    char digit = GetBaseDigitForValue(System.Convert.ToInt32(digitValue));
                    baseNumber += digit;
                }
                else
                {
                    baseNumber += "0";
                }

                _precision++;
            }

            if (isNegative)
            {
                baseNumber = StringMath.switch_sign(baseNumber);
            }

            return(StringMath.beautify_number(baseNumber));
        }