Use MemoryStream and BinaryWriter to convert decimal to byte array

image_pdfimage_print


   
 

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

           
         
     


MemoryStream.Write

image_pdfimage_print
   
  
using System;
using System.IO;

public class MemTest {
    public static void Main() {
        MemoryStream memOut = new MemoryStream();

        byte[] bs = { 1, 2, 3, 4, 5, 6 };
        memOut.Write(bs, 0, bs.Length);

        memOut.Seek(+3, SeekOrigin.Begin);
        byte b = (byte)memOut.ReadByte();
        Console.WriteLine("Value: " + b);
    }
}

   
     


Create a MemoryStream

image_pdfimage_print

using System;
using System.IO;

class MemStreamApp
{
static void Main(string[] args)
{
MemoryStream m = new MemoryStream(64);
Console.WriteLine(“Length: {0} Position: {1} Capacity: {2}”,m.Length, m.Position, m.Capacity);

for (int i = 0; i < 64; i++) { m.WriteByte((byte)i); } Console.WriteLine("Length: {0} Position: {1} Capacity: {2}",m.Length, m.Position, m.Capacity); byte[] ba = m.GetBuffer(); foreach (byte b in ba) { Console.Write("{0,-3}", b); } m.Close(); } } [/csharp]

CurrentSize and Scope

image_pdfimage_print
   
 

using System;
using System.IO;
using System.IO.IsolatedStorage;

class MainClass {
    static void Main(string[] args) {
        using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForAssembly()) {
            store.CreateDirectory("MyFolder");

            using (Stream fs = new IsolatedStorageFileStream("MyFile.txt", FileMode.Create, store)) {
                StreamWriter w = new StreamWriter(fs);
                w.WriteLine("Test");
                w.Flush();
            }

            Console.WriteLine("Current size: " + store.CurrentSize.ToString());
            Console.WriteLine("Scope: " + store.Scope.ToString());

            Console.WriteLine("Contained files include:");
            string[] files = store.GetFileNames("*.*");
            foreach (string file in files) {
                Console.WriteLine(file);
            }
        }

    }
}

    


GetFileNames, CreateDirectory, GetUserStoreForAssembly

image_pdfimage_print
   
 

using System;
using System.IO;
using System.IO.IsolatedStorage;

class MainClass {
    static void Main(string[] args) {
        using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForAssembly()) {
            store.CreateDirectory("MyFolder");

            using (Stream fs = new IsolatedStorageFileStream("MyFile.txt", FileMode.Create, store)) {
                StreamWriter w = new StreamWriter(fs);
                w.WriteLine("Test");
                w.Flush();
            }

            Console.WriteLine("Current size: " + store.CurrentSize.ToString());
            Console.WriteLine("Scope: " + store.Scope.ToString());

            Console.WriteLine("Contained files include:");
            string[] files = store.GetFileNames("*.*");
            foreach (string file in files) {
                Console.WriteLine(file);
            }
        }
    }
}

    


extends IDeserializationCallback

image_pdfimage_print
   
 
using System;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;


[Serializable]
public class BookRecord : IDeserializationCallback {
    public String title;
    public int asin;
    [NonSerialized]
    public int rank;

    public BookRecord(String title, int asin) {
        this.title = title;
        this.asin = asin;
        rank = GetSalesRank();
    }

    public int GetSalesRank() {
        Random r = new Random();
        return r.Next(5000);
    }

    public void OnDeserialization(Object o) {
        rank = GetSalesRank();
    }
}



public class StreamObject {
    public static void Main() {
        BookRecord book = new BookRecord("title", 123456789);
        Console.WriteLine(book.title + " " + book.asin +" " + book.rank);

        FileStream stream = new FileStream(@"book.obj", FileMode.Create);
        BinaryFormatter bf = new BinaryFormatter();
        bf.Serialize(stream, book);
        stream.Close();

        FileStream streamIn = new FileStream(@"book.obj", FileMode.Open);
        BookRecord bookIn = (BookRecord)bf.Deserialize(streamIn);
        Console.WriteLine(bookIn.title + " " + bookIn.asin + " " + bookIn.rank);
    }
}

    


Use GZipStream to Zip

image_pdfimage_print
   
 


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Compression;

class Program {
    static void SaveCompressedFile(string filename, string data) {
        FileStream fileStream = new FileStream(filename, FileMode.Create, FileAccess.Write);
        GZipStream compressionStream = new GZipStream(fileStream, CompressionMode.Compress);
        StreamWriter writer = new StreamWriter(compressionStream);
        writer.Write(data);
        writer.Close();
    }

    static string LoadCompressedFile(string filename) {
        FileStream fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
        GZipStream compressionStream = new GZipStream(fileStream, CompressionMode.Decompress);
        StreamReader reader = new StreamReader(compressionStream);
        string data = reader.ReadToEnd();
        reader.Close();
        return data;
    }

    static void Main(string[] args) {
        try {
            string filename = "compressedFile.txt";
            string sourceString = "Source String";
            SaveCompressedFile(filename, sourceString);
            FileInfo compressedFileData = new FileInfo(filename);
            string recoveredString = LoadCompressedFile(filename);
        } catch (IOException ex) {
            Console.WriteLine(ex.ToString());
        }
    }
}