Use MemoryStream and BinaryReader to convert Byte array to decimal : Memory Stream : File Stream C# Source Code


Custom Search

C# Source Code » File Stream » Memory Stream »

 

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));
    }
}


           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo File Stream
» Memory Stream