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






Use MemoryStream and BinaryWriter to convert decimal to byte array

Use MemoryStream and BinaryWriter to convert decimal to byte array
   

using System;
using System.IO;

class Test {
    // Create a byte array from a decimal.
    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 void Main() {
        byte[] b = DecimalToByteArray(285998345545.563846696m);
        // Convert a decimal to a byte array and display.
        Console.WriteLine(BitConverter.ToString(b));
    }
}

           
         
    
    
  








Related examples in the same category

1.Create a MemoryStream
2.MemoryStream.Write
3.Use MemoryStream and BinaryReader to convert Byte array to decimalUse MemoryStream and BinaryReader to convert Byte array to decimal
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