public static BigInteger VLEURead(byte[] buffer, ref int offset, bool isLittleEndian)
        {
            string read    = VLERead(buffer, ref offset, isLittleEndian);
            string reverse = StringUtil.Reverse(read);

            return(BigInteger.Parse(BaseConverter.Convert(reverse, 2, 10, 0, 0)));
        }
        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. 3
0
        public static string ReadFromByte(byte[] buffer, ref int bytepos, ref int littleendianbitpos, int bitnum, bool advanceTowardsLSB)
        {
            int overflow = GetOverflow(littleendianbitpos, bitnum, advanceTowardsLSB);

            if (bytepos <= buffer.Length - 1 && overflow == 0)
            {
                byte b = buffer[bytepos];

                if (advanceTowardsLSB)
                {
                    b   = (byte)(b << (8 - (littleendianbitpos + 1)));
                    b >>= (8 - (littleendianbitpos + 1));
                    b >>= (8 - (8 - (littleendianbitpos + 1) + bitnum));
                    littleendianbitpos = littleendianbitpos - bitnum;

                    if (littleendianbitpos == -1)
                    {
                        littleendianbitpos = 7;
                        bytepos++;
                    }
                }
                else
                {
                    b <<= (8 - (littleendianbitpos + bitnum));
                    b >>= (8 - (littleendianbitpos + bitnum));
                    b >>= littleendianbitpos;
                    littleendianbitpos %= 8;
                    bytepos++;
                }


                string binary = "";
                binary = BaseConverter.Convert(b.ToString(), 10, 2, 0, 0);

                // leading zeros are left out, must align bits to byte size
                binary = BitAlign(binary, bitnum);
                return(binary);
            }
            else
            {
                throw new Exception();
            }
        }
        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. 5
0
 public static byte stringToByte(string str)
 {
     return((byte)Convert.ToInt32(BaseConverter.Convert(str, 2, 10, 0, 0)));
 }