예제 #1
0
    public static void Main()
    {
        FileStream      fs;
        BinaryFormatter formatter = new BinaryFormatter();

        BigInteger number = BigInteger.Pow(Int32.MaxValue, 20) * BigInteger.MinusOne;

        Console.WriteLine("The original value is {0}.", number);
        SignAndMagnitude sm = new SignAndMagnitude();

        sm.Sign  = number.Sign;
        sm.Bytes = BigInteger.Abs(number).ToByteArray();

        // Serialize SignAndMagnitude value.
        fs = new FileStream(@".\data.bin", FileMode.Create);
        formatter.Serialize(fs, sm);
        fs.Close();

        // Deserialize SignAndMagnitude value.
        fs = new FileStream(@".\data.bin", FileMode.Open);
        SignAndMagnitude smRestored = (SignAndMagnitude)formatter.Deserialize(fs);

        fs.Close();
        BigInteger restoredNumber = new BigInteger(smRestored.Bytes);

        restoredNumber *= sm.Sign;
        Console.WriteLine("The deserialized value is {0}.", restoredNumber);
    }
예제 #2
0
        public void Case1()
        {
            FileStream      fs;
            BinaryFormatter formatter = new BinaryFormatter();
            Rational        number    = Math.Pow(Int32.MaxValue, 20) * Rational.MinusOne;

            Console.WriteLine("The original value is {0}.", number);
            SignAndMagnitude sm = new SignAndMagnitude();

            sm.Sign  = Math.Sign(number);
            sm.Bytes = Math.Abs(number).ToByteArray().Numerator;

            // Serialize SignAndMagnitude value.
            fs = new FileStream(@".\data.bin", FileMode.Create);
            formatter.Serialize(fs, sm);
            fs.Close();

            // Deserialize SignAndMagnitude value.
            fs = new FileStream(@".\data.bin", FileMode.Open);
            SignAndMagnitude smRestored = (SignAndMagnitude)formatter.Deserialize(fs);

            fs.Close();
            Rational restoredNumber = new Rational(false, smRestored.Bytes, new byte[] { 1 });

            restoredNumber *= sm.Sign;
            Console.WriteLine("The deserialized value is {0}.", restoredNumber);
        }