示例#1
0
        public static double ReadDouble(IntPtr from, int offset)
        {
            DoubleToLong dtl = new DoubleToLong();

            dtl.theLong = Marshal.ReadInt64(from, offset);
            return(dtl.theDouble);
        }
示例#2
0
        public static void Write(IntPtr to, int offset, double from)
        {
            DoubleToLong dtl = new DoubleToLong();

            dtl.theDouble = from;
            Marshal.WriteInt64(to, offset, dtl.theLong);
        }
        /// <summary>
        /// Method converts a double to IEEE 754 format.
        /// </summary>
        /// <param name="number">
        /// Source number.
        /// </param>
        /// <returns>
        /// Source number in IEEE 754 format.
        /// </returns>
        public string Transform(double number)
        {
            var numberOfBytes = 64;
            var doubleToLong  = new DoubleToLong()
            {
                PlaceForDouble = number
            };
            var numberToLong = doubleToLong.PlaceForLong;

            var doubleToIEEE754Format = new char[numberOfBytes];

            for (int i = numberOfBytes - 1; i >= 0; --i)
            {
                if ((numberToLong & 1) == 1)
                {
                    doubleToIEEE754Format[i] = '1';
                }
                else
                {
                    doubleToIEEE754Format[i] = '0';
                }

                numberToLong >>= 1;
            }

            return(string.Join(string.Empty, doubleToIEEE754Format));
        }
示例#4
0
        /// <summary>
        /// Doubles to binary converter
        /// </summary>
        /// <param name="number">The number.</param>
        /// <returns> Binary number </returns>
        public static string DoubleToBinaryString(this double number)
        {
            DoubleToLong value = new DoubleToLong {
                Double64bits = number
            };

            // Можно хоть в строку конвертировать?)
            return(String.Concat(Converter(value)));
        }
            public void Must_not_be_equal()
            {
                var value1 = new DoubleToLong(0.0);
                var value2 = new DoubleToLong((0.0 * 1.0) + 0.00001);

                Assert.That(value1 == value2, Is.False);

                value1 = new DoubleToLong(3.0 * 4.0);
                value2 = new DoubleToLong((2.0 * 6.0) + 0.00001);

                Assert.That(value1 == value2, Is.False);
            }
            public void Must_be_equal()
            {
                var value1 = new DoubleToLong(0.0);
                var value2 = new DoubleToLong(0.0 * 1.0);

                Assert.That(value1 == value2, Is.True);

                value1 = new DoubleToLong(3.0 * 4.0);
                value2 = new DoubleToLong(2.0 * 6.0);

                Assert.That(value1 == value2, Is.True);
            }
示例#7
0
        /// <summary>
        /// Performs the converting of double value to IEEE754 format.
        /// </summary>
        /// <param name="number">
        /// Value for converting to IEEE754 format.</param>
        /// <returns>
        /// String of bits from the source number.
        /// </returns>
        public static string DoubleToIEEE754(this double number)
        {
            int          sizeDoubleInBits = sizeof(double) * 8;
            DoubleToLong doubleInLong     = new DoubleToLong()
            {
                PlaceForDouble = number
            };
            long numberInLong = doubleInLong.PlaceForLong;

            char[] bitsOfNumber = new char[sizeDoubleInBits];

            for (int i = sizeDoubleInBits - 1; i >= 0; i--)
            {
                bitsOfNumber[i] = (numberInLong & 1) == 1 ? '1' : '0';
                numberInLong    = numberInLong >> 1;
            }

            return(new string(bitsOfNumber));
        }
示例#8
0
        private static byte[] Converter(DoubleToLong value)
        {
            byte[] doneNumber = new byte[64];
            long   mask       = 1L;

            for (int i = 0; i < 64; i++)
            {
                if ((value.Long64bits & mask) == 0)
                {
                    doneNumber[doneNumber.Length - 1 - i] = 0;
                }
                else
                {
                    doneNumber[doneNumber.Length - 1 - i] = 1;
                }

                mask <<= 1;
            }
            return(doneNumber);
        }
        /// <summary>
        /// Converts from double to string of it's binary representation.
        /// </summary>
        /// <param name="number">Double number.</param>
        /// <returns>Binary representation.</returns>
        public static string DoubleToBin(this double number)
        {
            string       bin          = null;
            DoubleToLong doubleToLong = new DoubleToLong(number);
            long         numberLong   = doubleToLong.Long64;

            for (int i = 0; i < 64; i++)
            {
                if ((numberLong & 1) == 1)
                {
                    bin += "1";
                }
                else
                {
                    bin += "0";
                }

                numberLong >>= 1;
            }

            return(Rev(bin));
        }
示例#10
0
        /// <summary>
        /// Public class for converting double value to its binary representation according standard IEEE754.
        /// </summary>
        /// <param name="number">Double value for converting.</param>
        /// <returns>String of bits.</returns>
        public static string ConvertToIEEE(double number)
        {
            DoubleToLong  doubleInLong = new DoubleToLong(number);
            long          bits         = doubleInLong.Long;
            StringBuilder resultString = new StringBuilder();

            for (int i = 0; i < bitsInLong; i++)
            {
                if ((bits & 1) == 1)
                {
                    resultString.Insert(0, "1");
                }
                else
                {
                    resultString.Insert(0, "0");
                }

                bits >>= 1;
            }

            return(resultString.ToString());
        }
示例#11
0
 public static double ReadDouble(IntPtr from, int offset)
 {
     DoubleToLong dtl = new DoubleToLong();
     dtl.theLong = Marshal.ReadInt64(from, offset);
     return dtl.theDouble;
 }
示例#12
0
 public static void Write(IntPtr to, int offset, double from)
 {
     DoubleToLong dtl = new DoubleToLong();
     dtl.theDouble = from;
     Marshal.WriteInt64(to, offset, dtl.theLong);
 }
示例#13
0
        public void CopyOut(IntPtr gapiPtr, int offset)
        {
            foreach (KeyValuePair <int, PropertyInvoker <short> > keyVal in propertyContainer.Int16Properties)
            {
                keyVal.Value.Set(Marshal.ReadInt16(gapiPtr, offset + keyVal.Key));
            }

            foreach (KeyValuePair <int, PropertyInvoker <ushort> > keyVal in propertyContainer.UInt16Properties)
            {
                keyVal.Value.Set((ushort)Marshal.ReadInt16(gapiPtr, offset + keyVal.Key));
            }

            foreach (KeyValuePair <int, PropertyInvoker <int> > keyVal in propertyContainer.Int32Properties)
            {
                keyVal.Value.Set(Marshal.ReadInt32(gapiPtr, offset + keyVal.Key));
            }

            foreach (KeyValuePair <int, PropertyInvoker <uint> > keyVal in propertyContainer.UInt32Properties)
            {
                keyVal.Value.Set((uint)Marshal.ReadInt32(gapiPtr, offset + keyVal.Key));
            }

            foreach (KeyValuePair <int, PropertyInvoker <long> > keyVal in propertyContainer.Int64Properties)
            {
                keyVal.Value.Set(Marshal.ReadInt64(gapiPtr, offset + keyVal.Key));
            }

            foreach (KeyValuePair <int, PropertyInvoker <ulong> > keyVal in propertyContainer.UInt64Properties)
            {
                keyVal.Value.Set((ulong)Marshal.ReadInt64(gapiPtr, offset + keyVal.Key));
            }

            foreach (KeyValuePair <int, PropertyInvoker <float> > keyVal in propertyContainer.SingleProperties)
            {
                //IntPtr newPtr = new IntPtr(gapiPtr.ToInt64() + offset + keyVal.Key);
                //keyVal.Value.Set((float)Marshal.PtrToStructure(newPtr, typeof(float)));
                SingleToInt sti = new SingleToInt();
                sti.theInt = Marshal.ReadInt32(gapiPtr, offset + keyVal.Key);
                keyVal.Value.Set(sti.theSingle);
            }

            foreach (KeyValuePair <int, PropertyInvoker <double> > keyVal in propertyContainer.DoubleProperties)
            {
                //IntPtr newPtr = new IntPtr(gapiPtr.ToInt64() + offset + keyVal.Key);
                //keyVal.Value.Set((double)Marshal.PtrToStructure(newPtr, typeof(double)));
                DoubleToLong dtl = new DoubleToLong();
                dtl.theLong = Marshal.ReadInt64(gapiPtr, offset + keyVal.Key);
                keyVal.Value.Set(dtl.theDouble);
            }

            foreach (KeyValuePair <int, PropertyInvoker <byte> > keyVal in propertyContainer.ByteProperties)
            {
                keyVal.Value.Set(Marshal.ReadByte(gapiPtr, offset + keyVal.Key));
            }

            foreach (KeyValuePair <int, PropertyInvoker <char> > keyVal in propertyContainer.CharProperties)
            {
                keyVal.Value.Set((char)Marshal.ReadByte(gapiPtr, offset + keyVal.Key));
            }

            foreach (KeyValuePair <int, PropertyInvoker <string> > keyVal in propertyContainer.StringProperties)
            {
                IntPtr stringPtr = Marshal.ReadIntPtr(gapiPtr, offset + keyVal.Key);
                keyVal.Value.Set(Marshal.PtrToStringAnsi(stringPtr));
            }

            foreach (KeyValuePair <int, PropertyInvoker <bool> > keyVal in propertyContainer.BoolProperties)
            {
                keyVal.Value.Set(Marshal.ReadByte(gapiPtr, offset + keyVal.Key) != 0);
            }

            // array type[] to gapi sequence<type>


            // jagged array type[][] to gapi sequence<sequence<type>>


            // fixed array type[] to gapi type[n]


            // multi-dim array type[,] to gapi type[][]


            // union
        }
示例#14
0
        public void CopyIn(IntPtr gapiPtr, int offset)
        {
            // short to gapi int
            foreach (KeyValuePair <int, PropertyInvoker <short> > keyVal in propertyContainer.Int16Properties)
            {
                Marshal.WriteInt16(gapiPtr, offset + keyVal.Key, keyVal.Value.Get());
            }

            // ushort to gapi unsigned int
            foreach (KeyValuePair <int, PropertyInvoker <ushort> > keyVal in propertyContainer.UInt16Properties)
            {
                Marshal.WriteInt16(gapiPtr, offset + keyVal.Key, (short)keyVal.Value.Get());
            }

            // int to gapi long
            foreach (KeyValuePair <int, PropertyInvoker <int> > keyVal in propertyContainer.Int32Properties)
            {
                Marshal.WriteInt32(gapiPtr, offset + keyVal.Key, keyVal.Value.Get());
            }

            // uint to gapi unsigned long
            foreach (KeyValuePair <int, PropertyInvoker <uint> > keyVal in propertyContainer.UInt32Properties)
            {
                Marshal.WriteInt32(gapiPtr, offset + keyVal.Key, (int)keyVal.Value.Get());
            }


            // long to gapi long long
            foreach (KeyValuePair <int, PropertyInvoker <long> > keyVal in propertyContainer.Int64Properties)
            {
                Marshal.WriteInt64(gapiPtr, offset + keyVal.Key, keyVal.Value.Get());
            }

            // ulong to gapi unsigned long long
            foreach (KeyValuePair <int, PropertyInvoker <ulong> > keyVal in propertyContainer.UInt64Properties)
            {
                Marshal.WriteInt64(gapiPtr, offset + keyVal.Key, (long)keyVal.Value.Get());
            }

            // float
            foreach (KeyValuePair <int, PropertyInvoker <float> > keyVal in propertyContainer.SingleProperties)
            {
                // floats are actually Struct Single
                // IntPtr doesn't allow pointer arithmetic, so convert to long and add
                //IntPtr newPtr = new IntPtr(gapiPtr.ToInt64() + offset + keyVal.Key);
                //Marshal.StructureToPtr(keyVal.Value.Get(), newPtr, true);

                SingleToInt sti = new SingleToInt();
                sti.theSingle = keyVal.Value.Get();
                Marshal.WriteInt32(gapiPtr, offset + keyVal.Key, sti.theInt);
            }

            // double
            foreach (KeyValuePair <int, PropertyInvoker <double> > keyVal in propertyContainer.DoubleProperties)
            {
                // doubles are actually Struct Double
                // IntPtr doesn't allow pointer arithmetic, so convert to long and add
                //IntPtr newPtr = new IntPtr(gapiPtr.ToInt64() + offset + keyVal.Key);
                //Marshal.StructureToPtr(keyVal.Value.Get(), newPtr, true);

                DoubleToLong dtl = new DoubleToLong();
                dtl.theDouble = keyVal.Value.Get();
                Marshal.WriteInt64(gapiPtr, offset + keyVal.Key, dtl.theLong);
            }

            // byte to gapi octet
            foreach (KeyValuePair <int, PropertyInvoker <byte> > keyVal in propertyContainer.ByteProperties)
            {
                Marshal.WriteByte(gapiPtr, offset + keyVal.Key, keyVal.Value.Get());
            }

            // char to gapi char (only 1 byte)
            foreach (KeyValuePair <int, PropertyInvoker <char> > keyVal in propertyContainer.CharProperties)
            {
                // Only use the lower 8 bits or lower byte.
                Marshal.WriteByte(gapiPtr, offset + keyVal.Key, (byte)keyVal.Value.Get());
            }

            // string to gapi string
            foreach (KeyValuePair <int, PropertyInvoker <string> > keyVal in propertyContainer.StringProperties)
            {
                IntPtr stringPtr = Marshal.StringToHGlobalAnsi(keyVal.Value.Get());
                Marshal.WriteIntPtr(gapiPtr, offset + keyVal.Key, stringPtr);
            }

            // bool
            foreach (KeyValuePair <int, PropertyInvoker <bool> > keyVal in propertyContainer.BoolProperties)
            {
                Marshal.WriteByte(gapiPtr, offset + keyVal.Key, keyVal.Value.Get() ? (byte)1 : (byte)0);
            }

            // array type[] to gapi sequence<type>


            // jagged array type[][] to gapi sequence<sequence<type>>


            // fixed array type[] to gapi type[n]


            // multi-dim array type[,] to gapi type[][]


            // union
        }