Exemple #1
0
        public static string GetValueFromBase(string number, int numberBase, int precision)
        {
            bool isNegative = false;

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

            string integer_part  = StringMath.get_integer_part(number);
            string fraction_part = StringMath.get_fraction_part(number);

            char[] integer_part_digits  = integer_part.ToCharArray();
            char[] fraction_part_digits = fraction_part.ToCharArray();
            string value = "0";

            for (int i = integer_part_digits.Length - 1, e = 0; i >= 0; i--, e++)
            {
                int charvalue = System.Convert.ToInt32(GetValueFromBaseDigit(integer_part_digits[i]));
                value = StringMath.add(new List <string> {
                    value, StringMath.multiply(new List <string> {
                        charvalue.ToString(), StringMath.exponentiate(numberBase.ToString(), e.ToString(), 0)
                    })
                });
            }

            for (int i = 0, e = -1; i < fraction_part_digits.Length; i++, e--)
            {
                int charvalue = System.Convert.ToInt32(GetValueFromBaseDigit(fraction_part_digits[i]));
                value = StringMath.add(new List <string> {
                    value, StringMath.multiply(new List <string> {
                        charvalue.ToString(), StringMath.exponentiate(numberBase.ToString(), e.ToString(), 0)
                    })
                });
            }

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

            return(value);
        }
        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);
            }
        }