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);
            }
        }
Esempio n. 2
0
 public static byte stringToByte(string str)
 {
     return((byte)Convert.ToInt32(BaseConverter.Convert(str, 2, 10, 0, 0)));
 }