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);
            }
        }
Пример #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));
        }
        public static byte[] DoubleToBytesCalc(double d, bool littleEndian)
        {
            if (Equals(d, 0))
            {
                return(new byte[8]);
            }
            else
            {
                byte[] buffer;

                if (double.IsPositiveInfinity(d))
                {
                    buffer    = new byte[8];
                    buffer[0] = 127;
                    buffer[1] = 240;

                    for (int i = 2; i < 8; i++)
                    {
                        buffer[i] = 0;
                    }
                }
                else if (double.IsNegativeInfinity(d))
                {
                    buffer    = new byte[8];
                    buffer[0] = 255;
                    buffer[1] = 240;

                    for (int i = 2; i < 8; i++)
                    {
                        buffer[i] = 0;
                    }
                }
                else
                {
                    bool   isNegative = false;
                    string binary     = "0";
                    binary = BaseConverter.Convert(d.ToString(), 10, 2, 32, 1076);

                    if (StringMath.is_negative(binary))
                    {
                        isNegative = true;
                        binary     = StringMath.switch_sign(binary);
                    }

                    string integer_part  = StringMath.get_integer_part(binary);
                    string fraction_part = StringMath.get_fraction_part(binary);
                    short  exponent      = 0;

                    if (!StringMath.are_equal(integer_part, "0"))
                    {
                        exponent = (short)(integer_part.Length - 1);
                        binary   = integer_part.Substring(1) + fraction_part;
                    }
                    else
                    {
                        exponent--;

                        for (int i = 0; i < fraction_part.Length; i++)
                        {
                            if (fraction_part[i] != '0')
                            {
                                binary = fraction_part.Substring(i + 1);
                                break;
                            }
                            else
                            {
                                exponent--;
                            }
                        }
                    }

                    exponent += 1023;
                    string _exponent = "0";
                    _exponent = BaseConverter.Convert(exponent.ToString(), 10, 2, 0, 0);
                    _exponent = StringUtil.PrependChar(_exponent, "0", 11 - _exponent.Length);
                    buffer    = new byte[8];
                    buffer[0] = BitUtil.SetBit(0, (int)BitUtil.BITS.EIGHTH, isNegative);
                    int bitpos  = 6;
                    int bytepos = 0;
                    buffer = BitUtil.WriteBits(buffer, ref bytepos, ref bitpos, _exponent, true);
                    int padding = 52 - binary.Length;

                    if (padding < 0)
                    {
                        binary = binary.Substring(0, 52);
                        binary = BaseConverter.Convert(binary, 2, 10, 0, 0);
                        binary = StringMath.add(new List <string> {
                            binary, "1"
                        });
                        binary  = BaseConverter.Convert(binary, 10, 2, 0, 0);
                        padding = 52 - binary.Length;
                        binary  = StringUtil.PrependChar(binary, "0", padding);
                    }
                    else
                    {
                        binary = StringUtil.AppendChar(binary, "0", padding);
                    }

                    buffer = BitUtil.WriteBits(buffer, ref bytepos, ref bitpos, binary, true);
                }

                if (!littleEndian)
                {
                    buffer = BitUtil.ReverseBuffer(buffer);
                }

                return(buffer);
            }
        }