MemoryStream.ToArray, MemoryStream.ReadDecimal : Memory Stream « File Stream « C# / C Sharp






MemoryStream.ToArray, MemoryStream.ReadDecimal

    

using System;
using System.IO;

class MainClass
{
    public static byte[] DecimalToByteArray (decimal src){
        using (MemoryStream stream = new MemoryStream()) {
           using (BinaryWriter writer = new BinaryWriter(stream)){
               writer.Write(src);
               return stream.ToArray();
           }
        }
    }
    public static decimal ByteArrayToDecimal (byte[] src){
        using (MemoryStream stream = new MemoryStream(src)){
           using (BinaryReader reader = new BinaryReader(stream)){
              return reader.ReadDecimal();
           }
        }
    }
    public static void Main()
    {
        byte[] b = null;

        b = BitConverter.GetBytes(true);
        Console.WriteLine(BitConverter.ToString(b));

        Console.WriteLine(BitConverter.ToBoolean(b, 0));

        b = BitConverter.GetBytes(3678);
        Console.WriteLine(BitConverter.ToString(b));

        Console.WriteLine(BitConverter.ToInt32(b, 0));

        b = DecimalToByteArray(285998345545.563846696m);
        Console.WriteLine(BitConverter.ToString(b));

        Console.WriteLine(ByteArrayToDecimal(b));
    }
    
}    

   
    
    
  








Related examples in the same category

1.Create a MemoryStream
2.MemoryStream.Write
3.Use MemoryStream and BinaryWriter to convert decimal to byte arrayUse MemoryStream and BinaryWriter to convert decimal to byte array
4.Use MemoryStream and BinaryReader to convert Byte array to decimalUse MemoryStream and BinaryReader to convert Byte array to decimal
5.Deep Copy with MemoryStream
6.Memory-Mapped Files
7.Deep clone with MemoryStream
8.Using MemoryStream to Serialize and Desirialize
9.Convert a string into an array of bytes