Use MemoryStream and BinaryReader to convert Byte array to decimal : Memory Stream « File Stream « C# / C Sharp






Use MemoryStream and BinaryReader to convert Byte array to decimal

Use MemoryStream and BinaryReader to convert Byte array to decimal
   
using System;
using System.IO;

class Test {
    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 = BitConverter.GetBytes(true);
        // Convert a decimal to a byte array and display.
        b = DecimalToByteArray(12345678987654321.563846696m);
        Console.WriteLine(BitConverter.ToString(b));

        // Convert a byte array to a decimal and display.
        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.MemoryStream.ToArray, MemoryStream.ReadDecimal
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